Search code examples
pythonfirebasefirebase-realtime-databasefirebase-admin

How to increment values in firebase realtime database python?


I have the exact same question as this one but in python :

How to increment values in Firebase Realtime Database (v9)

Is it actually implemented or not ? Because i am unable to find how to perform a :

from firebase_admin import db

realtime_db = db.reference(path="/", app=app, url="myurl")
realtime_db.update({
   f"chats/{uid}/num": db.increment(1),
})

Thanks in advance


Solution

  • From looking at the documentation of the Python Admin SDK there doesn't seem to be an implementation of the increment operation there.

    Luckily, that operation just gives you a sentinel value to send to the server, and you can create that yourself too based on the example in the documentation for the REST API. It should be something like this:

    realtime_db.update({
       f"chats/{uid}/num": { ".sv": { "increment": 1 }},
    })
    

    Syntax errors in the above example are definitely possible, so let me know about any of those you encounter and fix below please.