【问题标题】:Firestore: Query array with greater than operatorFirestore:具有大于运算符的查询数组
【发布时间】:2019-03-06 18:00:31
【问题描述】:

我在 Firestore 中有以下数据结构

- runners (Collection)
-- uid_1 (Document)
--- name: James
--- height: 170
--- weight: 54
--- runTimeInSeconds: [ 540, 533, 530, 500 ] // this is array of 4
-- uid_2 (Document)
--- name: Larry
--- height: 168
--- weight: 51
--- runTimeInSeconds: [ 521, 611, 501 ] // this is array of 3 runs
-- uid_3 (Document)
--- name: Ben
--- height: 180
--- weight: 76
--- runTimeInSeconds: [ 621, 533 ] // this is array of 2 runs

我想按此顺序获取以下文档(runTimeInSeconds 大于 600): uid_2 后跟 uid 3。但我不想看到 uid_1。

我应该编写什么 firestore 查询?

我试过了:

runnersRef.where("runTimeInSeconds", ">", 600) // this does not work.

【问题讨论】:

  • 您是在问您提出的查询是否有效?您是否尝试过仅运行它以查看它是否有效?
  • @DougStevenson 我的意思是问我应该写什么firestore查询。
  • 嗯,看起来您之前写过一篇,然后从问题中删除了它。它没有工作吗?看起来它会起作用的。如果您不清楚如何查询 Firestore,您应该从文档开始,而不是在 Stack Overflow 上询问。 firebase.google.com/docs/firestore/query-data/queries
  • @DougStevenson 我试过了。它似乎没有用。很奇怪。
  • @DougStevenson 它不工作。你可以查看stackblitz.com/edit/…,我该怎么做才能让它工作?

标签: firebase google-cloud-firestore


【解决方案1】:

您不能对列表中的值进行范围查询。为列表项创建的索引不是这样工作的。当使用 array_contains 操作符时,它只能检测列表中的值是否存在。如果您想使用范围运算符,您实际上需要将每个字段列表项拆分为一个新集合,每个项目都有单独的文档。每个文档都应该有一个字段,其中持续时间字段为数字。然后,您可以将其他字段复制到每个文档中,或者只为运行者存储对原始文档的引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多