Firestore:获取使用 where 找到的文档的子集合 [英] Firestore: Get subcollection of document found with where
问题描述
我正在尝试获取子集合中的文档,该子集合是使用 .where 函数找到的文档的一部分
I am trying to get the documents inside an subcollection which is part of an document found with the .where function
示例:
- RootCol/
- 文档 A/
- 子队列 1
- 文档 1
- 文档 2
- 文档 3
- 文档
- 子队列 1
- 文档 1
- 文档 2
- 文档 3
- 文档
我想从文档中获取
SubColl 1
下的所有文档,字段级别== 1I want to get all the documents under
SubColl 1
from the doc with the field level == 1我正在尝试这样做:
db.collection("RootColl").where("field", "==", "1").collection("SubColl 1").get()
但是这样做我得到了错误
But by doing that I get the error
未捕获的类型错误:db.collection(...).where(...).collection 不是函数
Uncaught TypeError: db.collection(...).where(...).collection is not a function
编辑 1:按照 Frank van Puffelen 的建议,我得到了同样的错误,collection"不是函数
EDIT 1: By following Frank van Puffelen suggestion, i get the same error, "collection" is not a function
推荐答案
一个子集合位于特定文档下.您现在共享的查询指向多个文档.您需要执行查询以确定它指向哪些文档,然后遍历结果,并获取每个文档的子集合.
A sub-collection lives under a specific document. A query as you've shared now points to a number of documents. You'll need to execute the query to determine what documents it points to, then loop over the results, and get the sub-collection for each document.
在代码中:
var query = db.collection("RootColl").where("field", "==", "1"); query.get().then((querySnapshot) => { querySnapshot.forEach((document) => { document.ref.collection("SubColl 1").get().then((querySnapshot) => { ... }); }); });
这篇关于Firestore:获取使用 where 找到的文档的子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 子队列 1