【问题标题】:How to get a collection from firestore with exception of one list [duplicate]如何从firestore获取一个列表之外的集合[重复]
【发布时间】:2018-11-26 09:26:24
【问题描述】:

我正在使用 firestore 来存储我的数据,并且我想从我的数据库中获取一组用户,除了一个(当前用户)。我该怎么做。

这是我的数据库的图像

在这里,我想获取除 ID 为“gi3oGztVrKQoiEHxFv28TAQpxah2”的用户之外的所有用户

我目前使用的代码是

this.donorCollection = this.fireStore.collection<any>('users').valueChanges(); console.log(this.donorCollection); this.donorCollection.subscribe(data => console.log(data) );

返回所有用户。这个特定任务的原因是我当前的用户总是在他/她的位置发生变化时更新他/她的数据库。因此,如果我将valuechanges() 应用于当前集合,它将始终提供新值,因为当前用户正在更新他/她的职位。

【问题讨论】:

    标签: angular firebase ionic3 google-cloud-firestore


    【解决方案1】:

    您不能使用 Firestore 查询排除文档。使用 Firestore 查询,您可以指定所有必须为真的条件(其中 x 和 y 和 z 等)。

    也就是说,如果您只想排除一个文档,那么在客户端过滤查询结果既简单又便宜。

    【讨论】:

    • 这里的电话字段是唯一的,有没有办法使用'电话'字段过滤掉它?
    • 请再次阅读我的回答 - “您不能使用 Firestore 查询排除文档”。
    【解决方案2】:

    只需从集合中过滤如下,

    this.donorCollection.subscribe((data) => {
     this.users = data.filter(person => person.id != 'gi3oGztVrKQoiEHxFv28TAQpxah2');
     console.log(this.users) ;
    });
    

    【讨论】:

      【解决方案3】:

      我可以通过参考这个来解决这个问题 -

      Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).
      

      因为我的phone 字段是唯一的。我通过如下调用查询解决了这个问题

      this.donorCollectionLR = this.fireStore.collection<any>('users', ref =>
            ref.where('phone', '<', '8086960113')).valueChanges();
            this.donorCollectionLR.subscribe(data =>{
              this.donorCollectionGR = this.fireStore.collection<any>('users', ref =>
              ref.where('phone', '>', '8086960113')).valueChanges();
              this.donorCollectionGR.subscribe(data2 =>{
                data2=data2.concat(data);
                observer.next(data2)
              });
            });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-06
        • 1970-01-01
        • 2023-03-25
        • 2018-03-28
        • 2020-06-03
        相关资源
        最近更新 更多