在每个文档中,我都有一个名为nameIndexes
的数组,它是一个关键字数组,类似于:
["cat", "christmas", "tree", "topper", "decoration"]
然后我有一系列搜索词,它们通过执行where查询来查找。我发现的是有人搜索了数组中不存在的内容,结果为空。
我搜索的代码如下:
this.ref = firebase.firestore().collection('designs').doc(firebase.auth().currentUser.uid).collection('designs');
try {
searchArray.forEach(a => {
this.ref = this.designCollection.where("nameIndexes", "array-contains", a)
})
const designs = await this.ref.get();
designs.forEach(doc => {
let design = doc.data();
design.id = doc.id;
d.push(design)
})
} catch (e) {
message.error(e.message)
}
一些例子:
场景1
搜索字词为["horse", "cat"]
它返回nameIndexes为["cat", "christmas", "tree", "topper", "decoration"]
场景2
搜索字词为["cat", "horse"]
未找到结果
我在这里假设的是,在场景1中,它在数组中没有发现任何马匹,因此继续执行cat并返回结果。在方案2中,它找到了猫,搬上了马,找不到了,所以什么也没返回。
有没有一种方法可以使其返回查询匹配的所有结果?我能想到的唯一方法是执行单独的where查询,然后将所有结果推入数组,但是,如果有多个搜索词,这将需要一段时间并为我的Firestore实例添加负载