Nikhil Chauhan
Nikhil Chauhan

Reputation: 61

how to add fields in google cloud fire store using python

i have two Json data data 1 and data 2 and i want to merge it in fire store document when i try to merge two dicts only half part is showing .

data = {
    u'name': u'Los Angeles',
    u'state': u'CA',
    u'country': u'USA'
}
data2= {
    u'name': u'MIAMI',
    u'state': u'CA',
    u'country': u'USA'
}

db.collection(u'cities').document(u'LA').set(data)



city_ref = db.collection(u'cities').document(u'LA')

city_ref.set({
    u'name': u'MIAMI',
    u'state': u'CA',
    u'country': u'USA'
}, merge=True)

 #only this part is showing
  {
    u'name': u'MIAMI',
    u'state': u'CA',
    u'country': u'USA'
}
   #when i, doing this
  data = {
    u'name': u'Los Angeles',
    u'state': u'CA',
    u'country': u'USA',
    u'name': u'MIAMI',
    u'state': u'CA',
    u'country': u'USA'}
    #only this much part is showing in my field
    u'name': u'MIAMI',
    u'state': u'CA',
    u'country': u'USA' 

is there any way to merge this two in python

Upvotes: 0

Views: 1476

Answers (2)

user5807443
user5807443

Reputation: 311

You can use this code( it will add the new fields if they don't exist and update them if they exist already).

doc.reference.update({
                u'newField1': newValue1
                u'newField2': newValue2
            })

Upvotes: 0

Juan Lara
Juan Lara

Reputation: 6854

You can only have one value per field name. Merge is useful when you want to add additional fields to the same document, for example:

 data = {
    u'name': u'Los Angeles',
    u'state': u'CA',
    u'country': u'USA'
}

db.collection(u'cities').document(u'LA').set(data)

city_ref = db.collection(u'cities').document(u'LA')

city_ref.set({
    u'name2': u'MIAMI',
    u'state2': u'CA',
    u'country2': u'USA'
}, merge=True)

# Result

data = {
    u'country': u'USA',
    u'country2': u'USA',
    u'name': u'Los Angeles',
    u'name2': u'MIAMI',
    u'state': u'CA',
    u'state2': u'CA',}

Upvotes: 3

Related Questions