Fire store Transaction - Field level increment not working as expected

151 views
Skip to first unread message

Saravanan Maruthanayakam

unread,
Feb 24, 2021, 5:36:29 AM2/24/21
to Firebase Google Group
Hi there,

Document structure:
Customer Collections-> Customer Name->Customer ID->Monthly usage Collection name->Month Id(Documents in it)-> Sub collection inside the month id->sub collection id->sub collection doc


Whenever a new doc is being inserted into the sub collection id the Month ID doc's field should be incremented. 

We have the firestore triggered cloud function for every sub collection doc write, the CF will perform this increment operation inside the Month ID doc.

The Month ID doc where the field  needs to be updated is inside the transaction, before incrementing the field previous value is read and incremented.

But the read of previous field is not up to time, so we miss some increments.

Like, on sub collection id 0 doc (10:27:27:113) create trigger ,the month id doc field count is updated to 1.

on sub collection id 1 doc (10:27:27:115) create trigger ,the CF is reading the previous count, instead of 1 the count comes as 0. so we miss the last increment.





Sam Stern

unread,
Feb 24, 2021, 6:13:11 AM2/24/21
to Firebase Google Group
Hi there,

If you're doing a numeric increment there is no need to do a read and then write, the best way is to use the special increment update operation:
https://firebase.google.com/docs/firestore/manage-data/add-data#increment_a_numeric_value

If you do need to read the data first (to make some sort of decision) then you will need to use a transaction:
https://firebase.google.com/docs/firestore/manage-data/transactions

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/62be0c2f-9794-4e3c-aec0-21b37e8a7444n%40googlegroups.com.

Saravanan Maruthanayakam

unread,
Feb 24, 2021, 8:20:42 AM2/24/21
to Firebase Google Group

Hi sam,

I tried with the increment suggestion. my code looks like,

docRef := client.Collection(Collection).Doc(10)).Collection(number).Doc(Id)

var err error

if strings.EqualFold(deletedFieldValue, "0") {
_, err = docRef.Update(ctx, []firestore.Update{
{Path: "Payload.StopCount", Value: firestore.Increment(1)},
})
}

if strings.EqualFold(deletedFieldValue, "1") {
_, err = docRef.Update(ctx, []firestore.Update{
{Path: "Payload.StopCount", Value: firestore.Increment(-1)},
})
}

On frequent doc creation the  stop count is inconsistent.

Saravanan Maruthanayakam

unread,
Feb 24, 2021, 11:10:09 AM2/24/21
to Firebase Google Group
Sam,

To make the question clear, whenever a new doc inserted/updated into the sub collection , the parent collection doc's field(stop count) should be incremented. 

I tried both the field increment and transaction update. 

At higher inflow rates maybe within a second if we receive around 10-15 sub collection records the parent doc's field is not incremented/decremented as expected.



Kato Richardson

unread,
Feb 24, 2021, 11:23:58 AM2/24/21
to Firebase Google Group
Hi Saravanan,

Please check out how to ask and creating an mcve. If you want troubleshooting help, you'll need to create a minimal repro of the issue, otherwise we'll just be guessing at what might be wrong with the code.

I'd also recommend that you ask this on Stack Overflow once you have a minimal repro.

☼, Kato



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages