【问题标题】:Querying by a field within type 'Reference' in Firestore在 Firestore 中按“参考”类型中的字段进行查询
【发布时间】:2021-12-29 09:32:46
【问题描述】:

假设你有一个这样的数据库(为了方便以 GraphQL 格式显示)

type User {
  id: ID!
  authenticated: Boolean
}

type Post {
  id: ID!
  user: User # 'Reference' type
}

如何查询经过身份验证的用户发布的帖子?
在 SQL 中,您可以将两个表连接在一起并使用 WHERE 子句来指定条件,例如WHERE users.authenticated = true

我想知道如何在 Firestore 中实现相同的查询。
如果我们在 Firestore 中无法做到这一点,那么这种查询的解决方案是什么?

【问题讨论】:

  • 绝对有可能。您使用的是什么编程语言?
  • @AlexMamo 我正在使用 JavaScript。
  • 您可以参考类似的Stackoverflow case。让我知道这是否有帮助!

标签: google-cloud-firestore


【解决方案1】:

是的,您可以在 Firestore 中使用类型作为“参考”来查询字段。引用是一种Cloud Firestore 支持的数据类型,如documentation。在项目中,引用可以指向任何其他集合中的任何其他文档。您可以像使用任何其他值一样在查询中使用引用:用于过滤、排序和分页 (startAt/startAfter)。

考虑一个简单的例子,有两个集合:集合 A 和集合 B。我们想找出 Collection A/PQR 的所有文档,它指的是一些非常具体的文档 Collection B/WJH强>。在这里,我们可以使用指向 Collection B 中文档的 Reference,然后使用 where 子句过滤 Collection A 的引用值的数据

在这里使用 Javascript:

// Create a reference to the specific document you want to search with:
 var reference = db.collection("Collection B").doc("WJH");

 // Construct a query that filters documents matching the reference:
 var query = db.collection("Collection A").where("reference", "==", reference);

您也可以参考类似的Stackoverflow case

【讨论】:

  • 谢谢,但这不是我想要的。我不会通过引用本身来查询,而是通过引用文档的字段(在我的示例中为 authenticated 字段)。
猜你喜欢
  • 1970-01-01
  • 2019-01-12
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
相关资源
最近更新 更多