Bino Thomas
Bino Thomas

Reputation: 100

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)

Upvotes: 4

Views: 2124

Answers (2)

Ahmed Amr
Ahmed Amr

Reputation: 589

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.

Upvotes: 2

ajorquera
ajorquera

Reputation: 1309

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

Upvotes: 5

Related Questions