【问题标题】:Inequality operator in where clause not working in firestorewhere 子句中的不等式运算符在 firestore 中不起作用
【发布时间】:2021-01-09 12:14:03
【问题描述】:

我想实时跟踪半径范围内的用户。我有一个 firestore 集合文档,其字段 userId 与我的 userId 不同。

这是代码

   this.watch = Plugins.Geolocation.watchPosition(
        { maximumAge: 0, timeout: 5000 },
        (geoPosition) => {
          if (geoPosition) {
            console.log(geoPosition);
            const field = "position";
            const center = this.geo.point(
              geoPosition.coords.latitude,
              geoPosition.coords.longitude
            );
            const usersRef = this.afs.firestore.collection('users');
            const nearbyusers = usersRef.where('userId', '!=', this.uid).orderBy('userId');
        
            const position = this.geo.point(
              geoPosition.coords.latitude,
              geoPosition.coords.longitude
            );
            usersRef.doc(this.uid).set({ position }, { merge: true });
            this.points = this.radius.pipe(
              switchMap((r) => {
                return this.geo.query(nearbyusers).within(center, r, field, {log: true});
              }),
              shareReplay(1)
            );
          }
        }
      );

在我的用户集合中,我有两个文档。但是在运行这段代码之后,我什么也没有得到。 没有错误。

【问题讨论】:

  • 不支持带有 != 子句的查询。在这种情况下,将查询拆分为大于查询和小于查询。例如,虽然不支持查询子句 where("age", "!=", "30"),但是可以通过组合两个查询得到相同的结果集,其中一个是 where("age", "", 30) firebase.google.com/docs/firestore/query-data/…
  • @vincentPHILIPPE 我试过了,但也没有用。
  • @vincentPHILIPPE 您可能应该根据您的评论写一个答案。
  • @vincentPHILIPPE 即使您“只是”进行了 Google 搜索,您也已经花费了一些时间来寻找 OP 问题的解决方案并用该解决方案撰写评论。所以我认为你应该得到积分!此外,写一个答案将使未来的读者清楚解决方案是什么(而不是阅读 cmets)。由于该问题的标题为“where 子句中的不等式运算符 在 Firestore 中不起作用”,因此未来的搜索可能会导致该问题。
  • @Midou 你用的是哪个版本的JS SDK?

标签: angular firebase ionic-framework angularfire geofirex


【解决方案1】:

我认为你应该尝试这样做:

不支持带有 != 子句的查询。在这种情况下,拆分 查询分为大于查询和小于查询。例如, 虽然不支持查询子句 where("age", "!=", "30"), 您可以通过组合两个查询来获得相同的结果集,一个与 子句 where("age", "", 30) Read more

所以对你来说,它会是这样的:

...
const nearbyusers = usersRef
                    .where('userId', '<', this.uid)
                    .where('userId', '>', this.uid)
                    .orderBy('userId')
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2016-11-13
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2018-01-21
    相关资源
    最近更新 更多