【问题标题】:Cloud Firestore query and limit doesn't query all dataCloud Firestore 查询和限制不会查询所有数据
【发布时间】:2020-11-04 12:11:00
【问题描述】:

我有一个标签列表,我想在其中搜索并检索与搜索匹配的数据。

我可以使用此查询正确检索所有数据

  searchTags(search: string): AngularFirestoreCollection<Tag> {    
    return this.afs.collection(this.dbPathTags, ref => ref.where('tag', "<=", search))
  }

但问题是我想将结果限制为 5,因为我打算在下拉/自动完成中显示它们(如 SO 中的标签)

添加限制后,查询只是从 db 中搜索前 5 个项目,如果它们匹配,则只返回其中任何一个。想要的结果是查询过滤所有项目然后返回 5 个项目

  searchTags(search: string): AngularFirestoreCollection<Tag> {    
    return this.afs.collection(this.dbPathTags, ref => ref.where('tag', "<=", search).limit(5))
  }

当搜索“a”时,我得到了这个结果

但是当搜索“m”或“math”时我什么都没有,注意“math”也在数据库中,并且在限制关闭时显示

限制关闭

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore angularfire


    【解决方案1】:

    已考虑将 startAt 与 Limit 而不是 where 一起使用。 Firebase 分页文档https://firebase.google.com/docs/firestore/query-data/query-cursors

    编辑:.orderBy('field', 'order') 当订单不存在时默认升序

    searchTags(search: string): AngularFirestoreCollection<Tag> {    
        return this.afs.collection(this.dbPathTags, ref => ref.orderby('field', 'order').startAt(search).limit(5))
      }
    

    也许是这样的?

    【讨论】:

    • 是的,确实有效!但是很高兴从哪里获得订单。之后我只需要自己过滤它。
    • 例如你可以。所以用 where 和 orderBy 过滤可见,然后 startBy 在 orderBy 前面添加 where
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2019-09-03
    相关资源
    最近更新 更多