[英]Firestore: Get subcollection of document found with where
我正在尝试获取子集合中的文档,该子集合是使用 the.where function 找到的文档的一部分
例子:
我想从字段级别 == 1 的文档中获取SubColl 1
下的所有文档
我正在尝试这样做:
db.collection("RootColl").where("field", "==", "1").collection("SubColl 1").get()
但是这样做我得到了错误
未捕获的类型错误:db.collection(...).where(...).collection 不是 function
编辑 1:按照 Frank van Puffelen 的建议,我得到了同样的错误,“集合”不是 function
子集合位于特定文档下。 您共享的查询现在指向多个文档。 您需要执行查询以确定它指向哪些文档,然后遍历结果,并获取每个文档的子集合。
在代码中:
var query = db.collection("RootColl").where("field", "==", "1");
query.get().then((querySnapshot) => {
querySnapshot.forEach((document) => {
document.ref.collection("SubColl 1").get().then((querySnapshot) => {
...
});
});
});
如果子集合包含3 个文档,如下所示:
collection > document > subcollection > document1 > field1: "value1", field2: "value2" document2 > field1: "value1", field2: "value2" document3 > field1: "v1", field2: "v2"
你可以得到子集合的2个文件有以下时:
import { query, collection, where, getDocs } from "firebase/firestore";
const q = query(collection(db, "collection/document/subcollection"),
where("field1", "==", "value1"));
const docsSnap = await getDocs(q);
docsSnap.forEach((doc) => {
console.log(doc.data()); // "document1" and "document2"
});
如果没有的forEach()方法来获取子集合的2个文件有以下时:
import { query, collection, where, getDocs } from "firebase/firestore";
const q = query(collection(db, "collection/document/subcollection"),
where("field1", "==", "value1"));
const docsSnap = await getDocs(q);
console.log(docsSnap.docs[0].data()); // "document1"
console.log(docsSnap.docs[1].data()); // "document2"
此外,您可以使用以下代码获取子集合的所有 3 个文档:
import { getDocs, collection } from "firebase/firestore";
const docsSnap = await getDocs(collection(db,"collection/document/subcollection"));
docsSnap.forEach((doc) => {
console.log(doc.data()); // "document1", "document2" and "document3"
});
没有forEach() 方法来获取以下子集合的所有 3 个文档:
import { getDocs, collection } from "firebase/firestore";
const docsSnap = await getDocs(collection(db, "collection/document/subcollection"));
console.log(docsSnap.docs[0].data()); // "document1"
console.log(docsSnap.docs[1].data()); // "document2"
console.log(docsSnap.docs[2].data()); // "document3"
同样,此外,您只能使用以下代码获取子集合的一个特定文档:
import { getDoc, doc } from "firebase/firestore";
const docSnap = await getDoc(doc(db, "collection/document/subcollection/document3"));
console.log(docSnap.data()); // "document3"
如果主集合中有多个文档,然后是子集合,则使用以下代码:
数据结构:users(collection)-->>user1(doc)-->group(collection)-->data(doc) users(collection)-->>user2(doc)-->group(collection)-- >数据(文档)
const mainRef = collection(db, "users"); //users is my main collection, containing all the users
const users = await getDocs(mainRef); //Getting all the users
users.forEach(async (user) => { // loop through each user so that we got id for each document(user) to get sub collection data
let userCollectionRef = collection(db, `users/${user.id}/groups`); //groups is my sub-collection name
const GroupDoc = await getDocs(userCollectionRef); //getting all docs for particular user in sub-collection(groups)
GroupDoc.forEach((item) => console.log(item.data())); // loop through each document in group for each user so that we got nested data
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.