Home > Blockchain >  how to use firestore wherefield method and adding document that collection to another collection
how to use firestore wherefield method and adding document that collection to another collection

Time:04-25

I am working a project to use shops. My structure data:

enter image description here

My code snippet

//current user sign in here and uid "VjZfKF1zmrV00C9VeTZxRmogybA2"
let currentUser = Auth.auth().currentUser
let db = Firestore.firestore()
db.collection("Blah").whereField("users", isEqualTo: currentUser?.uid).collection("otherPath").document().addDocument

I want to add data to use currentuser uid and if it is matching "Blah" inside documents then add that targeting document other collection.

.adddocumets or .setData it isn't allowed to firestore, so how can i figure it?

CodePudding user response:

There is no way to send a query and an update statement to Firestore in one go. You will instead have to:

  1. Execute a query to determine all documents that need to be updated.
  2. Loop over the results of that query in your application code.
  3. Update each document in turn inside that loop.

In addition, since users is an array field, you'll want to use the array-contains operator to match the correct documents.

  • Related