'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'



Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrey Mramorov
Solution 2 Julian