Firestore 查询中的多个"in"筛选器



我有一个方法,它接收用户输入,然后根据所选的过滤器查询Cloud Firestore数据库。但是为了实现我所需要的结果,我必须用多个";在";操作员。有人有变通办法吗?我的方法:

getGrouped: async function () {
let array = [];
const groupedQuery = query(
collectionGroup(db, "grouped"),
where("owner", "==", match.params.uid),
where("capaign", "in", this.capaign),
where("region", "in", this.region)
);
const groupedSnapshot = await getDocs(groupedQuery);
groupedSnapshot.forEach((doc: any) => {
array.push(doc.data());
});
return array;
},    

用户输入如下:["CA", "BO", "TX"]

Firestore每个查询只允许一个in条件。您需要在JavaScript中执行第二个操作来处理结果。

getGrouped: async function () {
let array = [];
const groupedQuery = query(
collectionGroup(db, "grouped"),
where("owner", "==", match.params.uid),
where("capaign", "in", this.capaign)
);
const groupedSnapshot = await getDocs(groupedQuery);
groupedSnapshot.forEach((doc: any) => {
if (this.region.includes(doc.get('region'))) {
array.push(doc.data());
}
});
return array;
},    

最新更新