首页 > 解决方案 > 如何使用多个 .Where() 进行搜索?Vue JS 和 Firebase

问题描述

我想查询多个集合的搜索,而不仅仅是品牌。我直接从 Cloud Firestore 查询

到目前为止,我成功查询了品牌。

我也尝试结合类别和描述,但总是失败

我使用 where() 直接从 firebase 调用

getExchanges({commit, state}, {searched} = {searched: ''}) {
      commit('resetPagination');
      // Here you want to make a call to firebase and ask for data
      return db
       .collection('exchanges')
       .where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')
       .limit(state.pagination.itemCount)
       .get()
       .then(snapshots => {
         if (snapshots.docs.length === 0) {
            commit('setExchanges', []);
            return []
         }
         const exchanges = snapshots.docs.map(doc => ({...doc.data(), id: doc.id}))
         commit('setExchanges', exchanges)
         commit('setLastItem', snapshots.docs[snapshots.docs.length - 1])
         commit('setPreviousFirstItem', snapshots.docs[0])
         return exchanges
        })

我在 firestore 交换集合中的字段是

对于类别,我尝试这样做

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

你有什么建议吗?这将是惊人的

干杯

扎伊德

标签: firebasevue.jsgoogle-cloud-firestoreconditional-statementswhere-clause

解决方案


目前 Cloud Firestore 不支持“OR”查询。您可以拥有的最接近的方法是使用“array-contains”和“array-contains-any”查询,并明确设计您的数据结构以满足查询需求。另一种方法是使用名为Algolia的第三方服务。

还有一点需要注意的是你的代码

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

等同于

.where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')

||如果第一项未定义、null、空字符串、空数组或 false,javascript 中的as运算符返回第二项,否则返回第一项。


推荐阅读