Home > Mobile >  How to auto increment document id in firestore based on the input number
How to auto increment document id in firestore based on the input number

Time:03-05

Average use case of the app is a form being filled by a surveyor, I have to produce a unique id for every entry based on three drop down selections, shown in the image below: Consumer check-in form

I have to produce a unique Consumer-Id for every entry based on zone and ward selection, as an example, the surveyor selects zone 01, and ward 02, and enters the first entry, the consumer id should be 01-02-01 respectively, AND when change the selection of either the zone or ward, the entry number should start from 1, and if they switch back to the zone and ward where they had made some entries before, let's say in zone 01, ward 02 they made 25 entries, so the last consumer id was 01-02-25, when they switch back to this zone and ward the entry should continue from 26.

I am unable to devise a logic for the above requirement, is it possible to achieve in firestore or locally inside the app?

CodePudding user response:

Assuming you have the fields zone and ward in your firestore documents, you can check how many entries have been made in that particular zone and ward by chaining two where clauses as shown below

Future<int> countEntriesInZoneWard(String zone, String ward) async {
    final QuerySnapshot result = await FirebaseFirestore.instance
    .collection('yourCollectionName')
    .where('Zone', isEqualTo: zone)
    .where("Ward", isEqualTo: ward)
            .get();
 final List<DocumentSnapshot> documents = result.docs;
    return documents.length;
}

The above code will return the length of the documents, or in other words, will give you a number of entries with the specified zone and ward, in order to generate a number for the new id, receive this method's returned value in a variable and add 1 to get the current entry number for the new id as follows:

 entryNum = await _db.countEntriesInZoneWard(zone, ward)   1;

Now you can append this variable entryNum at the end of your string for the Consumer Id

CodePudding user response:

These IDs that you're describing are known as sequences, because the ID of the next item depends on the previous item in the same sequence.

So, in order to determine the ID for the next item, you will either have to know the ID of the (so far) last item in the sequence. If all items are added by a single app instance/user, you can store the latest ID of each sequence in the local storage of the app. If items are added across devices/users, you will have to store the IDs in a database, such as Firestore, that all the devices can access.

With that, the sequence to add an item becomes:

  1. Read the latest ID from the sequence.
  2. Add the next item in the sequence.
  3. Write the incremented latest ID and next item.

If you're using a shared stored mechanism such as Firestore for this, you'll have to use its transaction mechanism to prevent users from generating the same ID.

  • Related