【问题标题】:Is there an equivalent to `beginsWith` in Firestore?Firestore 中是否有与 `beginsWith` 等效的内容?
【发布时间】:2018-04-09 08:12:06
【问题描述】:

Firestore documentation on making queries 包含一些示例,您可以根据文档中的某些字段是否匹配、小于或大于您传入的某个值来过滤文档集合。例如:

db.collection("cities").whereField("population", isLessThan: 100000)

这将返回“人口”小于 100000 的每个“城市”。这种类型的查询也可以在 String 类型的字段上进行。

db.collection("cities").whereField("name", isGreaterThanOrEqualTo: "San Francisco")

我没有看到执行子字符串搜索的方法。例如,这是不可用的:

db.collection("cities").whereField("name", beginsWith: "San")

我想我可以自己使用 greaterThanlessThan 添加类似的内容,但我想先检查一下:

  • 为什么不存在此功能?

我担心它不存在,因为性能会很糟糕。

【问题讨论】:

    标签: swift firebase google-cloud-firestore


    【解决方案1】:

    现在可以了:

    db.collection('cities')
    .where('name', '>=', 'San')
    .where('name', '<', 'Sam');
    

    更多详情见Firestore query documents startsWith a string

    【讨论】:

      【解决方案2】:

      [Googler here] 您说得对,Cloud Firestore 中没有像 beginsWithcontains 这样的字符串操作,您必须使用大于和小于比较来近似查询。

      您说“它不存在,因为性能会很糟糕”,虽然我不会使用您说对的那些确切词,但原因是性能。

      所有 Cloud Firestore 查询都必须命中索引。这就是我们如何保证任何查询的性能随着结果集的大小而扩展,即使在数据集增长时也是如此。我们目前没有以一种可以方便地为您想要的查询提供服务的方式对字符串数据进行索引。

      全文搜索操作是最重要的 Cloud Firestore 功能请求之一,因此我们肯定会对其进行调查。

      现在,如果您想进行全文搜索或类似操作,我们建议您与外部服务集成,我们提供了一些有关如何执行此操作的指导: https://firebase.google.com/docs/firestore/solutions/search

      【讨论】:

        猜你喜欢
        • 2018-11-09
        • 2021-11-09
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        相关资源
        最近更新 更多