Link Search Menu Expand Document

Updating Data

Table of contents

  1. Using Model Instance
  2. Using Key
  3. Update elements in an array
  4. Increment a numeric value
  5. Sub Collection

Update document in Firestore collection

Using Model Instance

Model instance has update() method you can update documents using this method

Example Usage

class User(Model):
    name = TextField()
    age = NumberField()

u = User(name="Azeem", age=26)

# Update document = "Arfan"

print(  # Arfan
print(u.age)  # 26

Using Key

Don’t get document just for updating it. This is not efficient way you can pass key directly to update() method.

Don’t Do This This will take two request one for getting data and second for updating

u = User.collection.get(user_key) = "Updated Name"

print(  # Updated Name
print(u.age)  # 26

If you don’t need to use document value then update the document just passing the key

Do This This will take only one request to update document which is efficient

u = User() = "Haider"

print(  # Haider
print(u.age)  # 26

Passing key is not always efficient

For example if you are creating new document and later you want to update it or if you getting document from query filter then passing key to update() is not make it efficient

For Example

user = User.collection.filter('name', '==', 'Azeem').get() = 'Update Name'

user.update(user.key)  # This will not make it efficient and this is not recommended way

# Instead of this use update method without passing key
user.update()  # Recommended way

Update NestedModel

But if you are using NestedModel then always first get() document then update otherwise it will create problem with default values if you set to any field. Because FireO can not know what value you want to add in updated document a new None value or the default value for this field. So that’s why always get document first then update

For Example

class Child(Model):
    amount = NumberField(default=7)

class Parent(Model):
    name = TextField()
    child = NestedModel(Child)

m = Parent() = 'Any Name'
m.child.amount = 10

# updating document 
# First get the document then update it without passing key in update method
m = Parent.collectino.get(key) = 'Updated Name'

Update elements in an array

If your document contains an array field, you can use ListUnion() and ListRemove() to add and remove elements. ListUnion() adds elements to an array but only elements not already present. ListRemove() removes all instances of each given element.

city = City.collection.get(city_key)

# Atomically add a new region to the 'regions' list field.
city.regions = fireo.ListUnion(['greater_virginia'])

# // Atomically remove a region from the 'regions' List field.
city.regions = fireo.ListRemove(['east_coast'])

Increment a numeric value

You can increment or decrement a numeric field value as shown in the following example. An increment operation increases or decreases the current value of a field by the given amount. If the field does not exist or if the current field value is not a numeric value, the operation sets the field to the given value.

city = City.collection.get(city_key)

city.population = fireo.Increment(50)

Sub Collection

Sub collection can also update in this same fashion