简体   繁体   中英

How to add element to array at firestore database? (python)

I need to add some value (val) to existing array in google.cloud.firestore document.

I found solution like that:

database.collection('collection_name').document('document_id').update({'array_name': firestore.ArrayUnion([val])})

where 'array_name' is name of array-type field in document with 'document_id' id in collection 'collection_name'.

That code used:

import firebase_admin
from firebase_admin import firestore

and database is:

database = firestore.client()

But PyCharm says:

Cannot find reference 'ArrayUnion' in 'firestore.py'

Seems like your imports are a little wrong. Try:

import firebase_admin 
from firebase_admin import firestore

You can also read more in the documentation here .

Full sample would be:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# Use a service account
cred = credentials.Certificate('path/to/serviceAccount.json')
firebase_admin.initialize_app(cred)

db = firestore.client()

I copied it straight from the docs .

The problem was in PyCharm Indexes, something like that. Code could run and ArrayUnion worked, but IDE said that there are no function ArrayUnion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM