【问题标题】:Get collections in subcollections and subcollections获取子集合和子集合中的集合
【发布时间】:2020-11-20 18:09:24
【问题描述】:
我的 Firestore 有问题。问题是我有这样一个数据库。我想下载 hives 集合中的所有东西。问题是每个养蜂场都有不同的集合。例如,我有:蜂房中的 3 个文档,每个蜂房和我拥有的每个文档中,例如:5 个蜂巢。我想要所有荨麻疹的所有细节。
users:
{uid}:
apiaries:
{uid}:
hives:
{uid}
notes:
{uid}
【问题讨论】:
标签:
firebase
vue.js
google-cloud-firestore
【解决方案1】:
如果您想获取所有名为“hives”的子集合中的所有文档,无论它们嵌套在哪里,那么您将需要使用collection group query。
firebase.firestore().collectionGroup("hives").get()
【解决方案2】:
这是我写的。这是相当正确的吗?我必须更改数据库中的任何内容吗?
async getHives() {
await fb.usersCollection
.doc(fb.auth.currentUser.uid)
.collectionGroup('hives')
.onSnapshot(snapshot => {
let hivesArray = [];
snapshot.forEach(doc => {
let hive = doc.data();
hive.id = doc.id;
hivesArray.push(hive);
});
store.commit('setHives', hivesArray);
});
},