我的集合中有两个字段,名为“uId”和“sellerUId”,如下面的截图所示。都是八月号。我要过滤uId == auth_id或sellerUId == auth_id。

我尝试了以下查询:
.doc('doc name')
.collection('collection name')
.where(['uId', 'sellerUId'], isEqualTo: 'auth_id')
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
print(doc.id);
}
});发布于 2022-09-19 17:17:23
Firestore不支持多个字段的OR查询。您需要两种不同的查询--一个用于uid == auth_id,另一个用于sellerUid == auth_id。
对于这个用例,您可以在文档users中存储一个数组,该数组包含这样的userId和sellectUid,因此您可以在一个查询中获取这些文档:
{
users: ["uid", "sellerUid"],
...otherFields
}然后可以使用array-contains操作符:
.doc('doc name')
.collection('collection name')
.where("users", arrayContains: "auth_id");
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
print(doc.id);
}
});这将获取users数组包含auth_id的所有文档。您可以使用以下安全规则(假设auth_id是当前用户的UID )来保护这个安全:
match /collection/{docId} {
allow read: if request.auth.uid in resource.data.users;
}https://stackoverflow.com/questions/73776952
复制相似问题