Cloud Firestore 查询和限制不查询所有数据

Cloud Firestore query and limit doesn't query all data

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

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

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

但问题是我想将结果限制为 5,因为我打算在 dropdown/autocomplete 中显示它们(如 SO 中的标签)

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

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

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

但是在搜索“m”或“math”时我没有得到任何东西,请注意“math”也在数据库中并且在限制关闭时显示

关闭限制

考虑过将 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))
  }

也许是这样的?