如果您这样查询:
where("members.account_id", "==", "qgZ564nfEaNP3Nt4cW1K3jCeVlY2")
Firebase会检查给定值是否存在嵌套字段/members/account_id
。而这样的字段不存在,因为实际上该值在/members/0/account_id
。
由于你不知道数组中ID存在的位置,因此你需要使用array-contains
条件。使用array-contains
Firestore会检查包含您指定的确切成员的数组。
使用当前结构,您可以通过以下方式检查成员是否存在:
.where("members", "array-contains", { accountId: "qgZ564nfEaNP3Nt4cW1K3jCeVlY2", approved: true })
使用array-contains
需要知道数组元素的全部数据。
如果你只知道单个成员,而你存储了多个成员,那么就不能使用array-contains
。在这种情况下,我建议为每个文档添加一个额外的member_accountIds
字段,其中仅存储他们的账户ID:
member_accountIds: ["qgZ564nfEaNP3Nt4cW1K3jCeVlY2", ... ]
当这个步骤完成后,你就可以像这样进行查询:
.where("members_accountIds", "array-contains", "qgZ564nfEaNP3Nt4cW1K3jCeVlY2")
cloud_firestore
还是firebase
在pub.dev上? - Benjaminhttps://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
- Parth Bhanderi