在Cloud Firestore中查找具有特定参考的所有文档 [英] Finding all docs with specific reference in Cloud Firestore
问题描述
在Cloud Firestore上,我以这种方式拥有参考其他文档的文档:
On Cloud Firestore I have documents with reference to another document this way:
在我的示例中,文档Collection A/WJQ9yx67RrqHWQoEp0e2
指的是文档Collection B/rFOEwdw5go4dbitOCXyC
,但是当然,可能有无限多个文档指的是所提到的文档.
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.
现在,我想找出Collection A
的所有文档,这些文档都引用了非常具体的文档Collection B/rFOEwdw5go4dbitOCXyC
.
Now I would like to find out all the docs of Collection A
which are referring to this very specific document Collection B/rFOEwdw5go4dbitOCXyC
.
这怎么可能?我该如何实现?
与此有关的Firebase文档尚不清楚.
Documentation of Firebase is bit unclear with this.
推荐答案
很对,不幸的是,在文档中没有实际使用Reference
数据类型的示例,实际上唯一提及的是在受支持的数据类型部分.
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.
尽管最终,Reference
可以像Firestore中可用的任何其他数据类型一样使用,因此可以用于过滤&也可以对数据进行排序.
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.
要实现此目标,您需要构造一个指向Collection B
中文档的Reference
,然后使用where
子句来过滤Collection A
的reference
值上的数据.例如,在JavaScript中:
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);
查看 Firebase JavaScript SDK中isEqual()
的源,通过简单地检查路径是否匹配来执行Reference
(扩展为Query
)的比较:
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;
}
这似乎很像在两者上都调用toString()
并比较字符串值.
This would seem to be much like calling toString()
on both and comparing the string values.
昨天我制作了一个类似示例,这使我测试了存储Reference
的可能用途,以便也可以在这里申请.
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屋!