【问题标题】:Angularfire Query based on multiple where same column基于多个相同列的Angularfire查询
【发布时间】:2019-09-11 05:12:50
【问题描述】:

大家好,我有一个问题。

可以将同一列不同值的查询加倍

this.afs.collection ( 'boys', ref => {
     return ref.orderBy ( 'firstName', 'asc' )
               .where ( 'place', '==', 'home')
               .where ( 'place', '==', 'school' );

boys: [
 1: {firstName:'Peter', place:'home'},
 2: {firstName:'Martha', place:'home'},
 3: {firstName:'Juan', place:'school'},
 4: {firstName:'Charles', place:'park'},
 5: {firstName:'Andrew', place:'garden'},
 6: {firstName:'Maria', place:'school'},
]

预期价值

[
 2: {firstName:'Martha', place:'home'},
 1: {firstName:'Peter', place:'home'},
 3: {firstName:'Juan', place:'school'},
 6: {firstName:'Maria', place:'school'},
]

顺序无关

【问题讨论】:

  • 不知何故我不明白你的意思。您想先按名字然后按地点订购吗?
  • @ConstantinBeer 真的排序并不重要,我想要的是搜索包含两个或多个不同值的列,它类似于 sql 中的SELECT IN
  • place 是序数还是位置?可以把地方换成数字中的1,还是你的测试数据和一个地方最终会变成“大学校园”?
  • @cutiko我无法将值更改为数字,它们必须是文本
  • 为什么这些值必须是文本?这些值不是代表数字吗?

标签: firebase google-cloud-firestore angularfire2 angularfire


【解决方案1】:

我不知道是否有办法在一个查询中完成。但是您可以做的是使用两个单独的查询,然后将它们组合起来。

这是一个例子:

const resultA = this.afs.collection ( 'boys', ref => {
     return ref.orderBy ( 'firstName', 'asc' )
               .where ( 'place', '==', 'one');


const resultB = this.afs.collection ( 'boys', ref => {
     return ref.orderBy ( 'firstName', 'asc' )
               .where ( 'place', '==', 'two' );

const resultAB = combineLatest(resultA, resultB).pipe(
    map(([boysA, boysB]) => boysA.concat(boysB)));

【讨论】:

  • 这是正确的,firestore 不支持 OR like 运算符 :)
猜你喜欢
  • 2016-04-22
  • 1970-01-01
  • 2012-02-19
  • 2013-12-09
  • 1970-01-01
  • 2017-11-02
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多