我想在集合中找到一个文档,然后将项目添加到子集合(可能尚不存在):
projects (collection)
project (doc)
cluster (collection) // might not exist
node1 (doc) // might not exist
statTypeA (collection) // might not exist
我希望有这样的东西:
// Know the doc:
db.ref(`projects/${projectId}/cluster/node1/${statType}`).add()
// Or filter and ref:
db.collection('projects').where(..).limit(1).ref(`cluster/node1/${statType}`).add()
我最终以这种方式解决了它,但是它丑陋,冗长且缓慢,因为它必须首先返回许多读取操作。我这样做对吗?
const projectRefs = await db.collection('projects')
.where('licenseKey', '==', licenseKey)
.limit(1)
.get();
if (!projectRefs.docs) {
// handle 404
}
const projectRef = projectRefs.docs[0].ref;
const cluster = await projectRef.collection('cluster')
.doc('node1').get();
await cluster.ref.collection(statType).add({ something: 'hi' });
编辑:
我最终以更好的方式处理此问题的方法是将扁平化到其他集合,也将数组用于统计信息。感觉好多了:
// projects
{
projectId1
}
// instances (to-many-relationship) (filter based on projectId)
{
projectId
statTypeA: []
statTypeB: []
}
皈依舞
相关分类