stackUser
stackUser

Reputation: 13

in Cloud Firestore transaction how Check to see if a Document exists in a specific Collection if we don't know Document name

in cloud firestore transactions how can we find out if a document exists in a Collection when we just know a field name and its value and not the document name itself ?

*i know the Collection name

*i do not know the document name

*i know the field_name and field_value that i'm looking for

for example if i were not in transactions i could do it in the below way in python

def if_Exists(self):
    db = firestore.client()
    docs = db.collection(u'cities').where(u'field_name', u'==', 'field_value').get()

    for doc in docs:
        if bool(doc.to_dict()):
            return True
        return False

but in transactions for example in the below code sample(from Google docs) i don't know how can i use the where method(that i used in if_Exists() method)

transaction = db.transaction()
city_ref = db.collection(u'cities').document(u'SF')

@firestore.transactional
def update_in_transaction(transaction, city_ref):
    snapshot = city_ref.get(transaction=transaction)
    transaction.update(city_ref, {
        u'population': snapshot.get(u'population') + 1
    })

update_in_transaction(transaction, city_ref)

maybe the only way is to change my document names to my field values but i wanted to make sure that if its the only way ? Thank You

Edit for more detail: actually i wanted to only check if that document exists or not so i can prevent adding duplicate ones in my transaction. if i do it outside my transaction which i already did it. it does not work.

for example if i run my transaction from two computer at the same time finally i got duplicate documents in my collection with the same field_name and field_value. i thought i could solve this problem with doing query in transaction and return from transaction if i already had that document.

so as Doug Stevenson said its impossible so i'm curious why is the exact restriction that makes its impossible to have this feature in firestore ? is it impossible in SQL databases too ? is querying in transaction breaking an important rule or something ?

Upvotes: 1

Views: 1028

Answers (2)

Shrish Sharma
Shrish Sharma

Reputation: 1

For the first part of question: Doug has the answer

For the edited part, you can check this out: Go - check Firestore document exists The answer is specific to Golang but the same can be found for the other languages supported by firestore

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317392

It sounds like what you're trying to do is effectively a query inside a transaction in order to find the documents you want to transact on. This isn't possible in Firestore. You have to know exactly which documents you want in the transaction ahead of time.

You should probably query first for the documents whose fields match what you want, collect those document references into an array, then iterate that array inside the transaction handler to make changes to them as needed.

Upvotes: 1

Related Questions