0

I want to check specific field if its duplicate in another document or not before I add document to my collection

await FirebaseFirestore.instance.collection('Cars').add({
        'Car Owner Name': 'uj',
        'Car Make': CarMake,
        'Car Name': CarName,
        'Car Year': CarYear,
        'Plate Number': PlateNumber,
      });

the field is Plate Number

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
iKaka
  • 201
  • 1
  • 3
  • 8

1 Answers1

1

You can use PlateNumber as the document ID. If a car with that plate number already exists, it will be updated.

await FirebaseFirestore.instance.collection('Cars').doc(PlateNumber).set({
    'Car Owner Name': 'uj',
    'Car Make': CarMake,
    'Car Name': CarName,
    'Car Year': CarYear,
    'Plate Number': PlateNumber,
});

However, there are some constraints on document IDs. I guess the only problem will be forward slashes (the / character) that some plate numbers might have. You can replace any forward slashes in the document ID with some other character that would never appear in a plate number. I would use the % symbol, because it looks like a forward slash, but with 2 extra circles :-)

Stratubas
  • 2,939
  • 1
  • 13
  • 18