Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use firestore field incremental using python?

I am looking for a way to increment and update a firestore field in Python.
Seems like incremental feature is added in recently google-cloud-firestore 1.2.0 . issue-7533! How can this be implemented in python? Any sample implementation reference code will be really helpful.

from google.cloud import firestore
......
upd_ref_path.update({u'test_col': firestore.FieldValue.increment(1)})

I got following error message error: AttributeError: module 'google.cloud.firestore' has no attribute 'FieldValue'

UPDATE -

This feature needs an enchantment to use if from firestore module. Issue - issue-8173! is opened for this.

For temp solution use as below:

from google.cloud.firestore_v1 import Increment
doc_ref.set({u'test_col': Increment(1)}, merge=True)
like image 243
Bino Thomas Avatar asked Sep 15 '25 10:09

Bino Thomas


2 Answers

Check the documentation. You should be able to use it like:

from google.cloud import firestore
......
upd_ref_path.update({u'test_col': firestore.transforms.Increment(1)})

Hope that helps

like image 92
ajorquera Avatar answered Sep 18 '25 09:09

ajorquera


If you're importing from firebase_admin it should be like this

washington_ref = db.collection(u'cities').document(u'DC')

washington_ref.update({"population": firestore.Increment(50)})

Check docs for more info.

like image 23
Ahmed Amr Avatar answered Sep 18 '25 09:09

Ahmed Amr