Home > Enterprise >  Firestore do not create
Firestore do not create

Time:09-21

I am writing a Python code connected to Cloud Firestore. I want my code to skip and not create a document if the document's id is 1

if docid == 1:
    # do not create document

else:
   db.collection(u'collection_of_work').document(docid).set(data)

How to "not create" a document?

CodePudding user response:

There is no way you can do that automatically. You have to check that. So you need to create a reference that points to the document with a particular ID and read it. If it already exists, then simply increment the number by one and create another set() operation.

CodePudding user response:

I think you can use an if statement for that (I assume you know the docid since you want to set the doc using that docid):

if docid != 1:
    db.collection(u'collection_of_work').document(docid).set(data)

CodePudding user response:

Try with firebase-admin

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate(
        "firestore-cred.json"
    )

firebase_admin.initialize_app(cred)

db = firestore.client()  

query_docid = db.collection(u'collection_of_work').document(u"1", u"==", docid).get()
    
if query_docid:
    print('do not create docuement')
else :
    db.collection(u'collection_of_work').document(docid).set(data)
                

My problem also was similar to this. I learnt the answer by Barış Şenyerli

If you can't find the answer this guy will help you.

All the best

  • Related