iKaka
iKaka

Reputation: 261

how to check if field is duplicate in Another doc (Firebase flutter)

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

Upvotes: 0

Views: 767

Answers (1)

Stratubas
Stratubas

Reputation: 3067

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 :-)

Upvotes: 1

Related Questions