如何使用 firestore wherefield 方法并将该集合的文档添加到另一个集合

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

我正在做一个使用商店的项目。我的结构数据:

我的代码片段

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

我想添加数据以使用当前用户 uid,如果它与文档中的“Blah”匹配,则将该目标文档添加到其他集合。

.adddocumets.setData 不允许 firestore,所以我怎么知道呢?

无法一次性向 Firestore 发送查询和更新语句。您将不得不:

  1. 执行查询以确定需要更新的所有文档。
  2. 在您的应用程序代码中循环查询该查询的结果。
  3. 在该循环内依次更新每个文档。

此外,由于 users 是一个数组字段,您需要 use the array-contains operator 来匹配正确的文档。