firebase - 如何使用多个 .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')
你有什么建议吗?这将是惊人的
干杯
扎伊德
解决方案
目前 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运算符返回第二项,否则返回第一项。
推荐阅读
- angular - 如何在 Angular 8 项目中实现数据表属性 stateSave: boolean?我在我的项目中使用 angular2-datatable
- python-3.x - 如何在 Django 3.9 环境中运行独立的 python 2 应用程序
- java - 当 Java APM 代理在 Docker 中运行时,如何将 Java APM get 连接到 APM 服务器?
- javascript - AEM 6.5 如何将 content.xml 数据传递给反应组件
- python - Pandas read_csv 读取文件中不存在的浮动值
- javascript - 颤振 | FireStore 错误:RangeError(索引):索引超出范围:索引应小于 1:1
- java - Maven:如何通过 Fiddler 捕获 HTTPS 流量
- html - 如何删除这些 CSS 行之间的间距?
- javascript - 在 ajaxComplete 上获取 DOM
- javascript - 如何使用从方法参数推断的类型创建自定义 Svelte 存储,并且每次反应变量更改时都不会破坏它?