在 Cloud Firestore 中查找具有特定参考的所有文档 [英] Finding all docs with specific reference in Cloud Firestore
问题描述
在 Cloud Firestore 上,我的文档以这种方式引用了另一个文档:
在我的示例中,文档 Collection A/WJQ9yx67RrqHWQoEp0e2
指的是文档 Collection B/rFOEwdw5go4dbitOCXyC
,但当然,可以无限引用提到的文档.
现在我想找出 Collection A
的所有文档,这些文档涉及这个非常具体的文档 Collection B/rFOEwdw5go4dbitOCXyC
.
这怎么可能?我如何才能做到这一点?
Firebase 的文档对此有点不清楚.
你说得对,很遗憾,文档中没有实际使用 Reference
数据类型的例子,事实上唯一的支持的数据类型部分中提到了它.
最终,Reference
可以像 Firestore 中可用的任何其他数据类型一样使用,因此可用于过滤 &也对数据进行排序.
要实现您的目标,您需要构造一个 Reference
指向 Collection B
中的文档,然后使用 where
子句根据 Collection A
的 reference
值过滤数据.例如在 JavaScript 中:
//创建对要搜索的特定文档的引用var reference = db.collection("Collection B").doc("rFOEwdw5go4dbitOCXyC");//构造一个查询来过滤与引用匹配的文档var query = db.collection("Collection A").where("reference", "==", reference);
查看 isEqual()
在 Firebase JavaScript SDK 中的源代码,Reference
(扩展 Query
)的比较是通过简单的检查来执行的路径匹配:
isEqual(other: Query): boolean {//[...]const sameRepo = this.repo === other.repo;const samePath = this.path.equals(other.path);const sameQueryIdentifier =this.queryIdentifier() === other.queryIdentifier();返回 sameRepo &&相同路径 &&相同的查询标识符;}
这看起来很像在两者上调用 toString()
并比较字符串值.
我昨天制作了一个类似示例,这让我测试了存储参考资料的可能用途代码>,所以这也适用于这里.
On Cloud Firestore I have documents with reference to another document this way:
In my example, document Collection A/WJQ9yx67RrqHWQoEp0e2
is referring to document Collection B/rFOEwdw5go4dbitOCXyC
, but there of course, could be infinitely docs referring to one mentioned.
Now I would like to find out all the docs of Collection A
which are referring to this very specific document Collection B/rFOEwdw5go4dbitOCXyC
.
How is this possible? How I can achieve this?
Documentation of Firebase is bit unclear with this.
You're right, there's unfortunately no example of actually making use of a Reference
data type in the documentation, in fact the only a mention of it is in the Supported Data Types section.
Ultimately though, a Reference
can be used just like any other data type available in Firestore, so can be used to filter & sort data too.
To achieve what you're after, you would need to construct a Reference
that points to the document in Collection B
and then use a where
clause to filter data on the reference
value of Collection A
. For example in JavaScript:
// Create a reference to the specific document you want to search with
var reference = db.collection("Collection B").doc("rFOEwdw5go4dbitOCXyC");
// Construct a query that filters documents matching the reference
var query = db.collection("Collection A").where("reference", "==", reference);
Looking at the source for isEqual()
in the Firebase JavaScript SDK, comparing of a Reference
(extends Query
) is performed by simply checking that the paths match:
isEqual(other: Query): boolean { // [...] const sameRepo = this.repo === other.repo; const samePath = this.path.equals(other.path); const sameQueryIdentifier = this.queryIdentifier() === other.queryIdentifier(); return sameRepo && samePath && sameQueryIdentifier; }
This would seem to be much like calling toString()
on both and comparing the string values.
I produced a similar example yesterday which lead me to test the possible uses of storing a Reference
, so that may also apply here.
这篇关于在 Cloud Firestore 中查找具有特定参考的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!