Firestore-特定文档的子集合查询 [英] Firestore - subcollection query for a specific document
问题描述
注意:Cloud Firestore当前不支持跨子集合查询.如果您需要跨集合查询数据,请使用根级别的集合.
Note: Querying across subcollections is not currently supported in Cloud Firestore. If you need to query data across collections, use root-level collections.
当我为我阅读文档时,尚不清楚是否可以执行以下操作:
When I read the documentation for me it's not clear if it's possible to do something like this:
var listsRef = db.collection("users/user1/lists");
var query = listsRef.where("public", "==", true);
我可以理解,如果我尝试执行此操作,则不受支持:
I can understand that it's not supported if I try to do this:
var listsRef = db.collection("users/{userId}/lists");
var query = listsRef.where("public", "==", true);
但是我想知道是否可以查询来自特定文档的特定集合.实际上,这里和根集合有什么区别?
But I am wondering if I can query in a specific collection that comes from a specific document. In fact, what's the difference here and in a root collection?
谢谢.
推荐答案
SDK现在支持此功能
请在下面查看其他答案...
Please see the other answers below...
旧答案
只要知道子集合所属的文档,就可以对其进行查询.例如,以下是有效的子集合查询:
You can query on a subcollection as long as you know the document it belongs to. For example the following is a valid subcollection query:
const usersCollection = firestore.collection("users");
const adminDocument = usersCollection.doc("admin");
const adminsFollowersQuery = adminDocument.collection("followers").where("name", "==", "Arthur Dent");
adminsFollowersQuery.get().then((adminsFollowers) => {
adminsFollowers.docs.forEach((adminFollower) => {
console.log(adminFollower.get("name"));
});
});
如您所指出,以下内容当前无效:
As you point out the following is NOT currently valid:
const allUserFollowersQuery = firestore.collection("users/{wildcard}/followers")
.where("name", "==", "Arthur Dent");
adminsFollowersQuery.get().then((allUserFollowers) => {
allUserFollowers.docs.forEach((follower) => {
console.log(follower.get("name"));
});
});
我认为这里的文档有些混乱,但是它们的意思是通过查询子集合",您可以查询特定的子集合,如上所述,但是您不能查询常规子集合.至少不是使用FireStore SDK的单个操作.
I think the documentation here is a little confusing but by "Querying across subcollections" they mean, you can query a specific subcollection, as above, but you cannot query general subcollections. At least not as a single action using the FireStore SDK.
这篇关于Firestore-特定文档的子集合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!