一次创建包含子集合的doc [英] Create doc with subcollection in one shot
问题描述
假设我有一个名为root的集合
Let's say I have collection called root
我可以在一次调用中创建带有子集合的文档吗?
Can I create document with its subcollection in one call?
我的意思是:如果我这样做:
I mean if I do:
db.Collection("root").document("doc1").Collection("sub").document("doc11").Set(data)
然后一次性创建结构?
说实话,我试了一下,doc1有一个斜体标题,我认为只有删除的文件
Then will that create the structure in one shot? To be honest I gave this a try and doc1 had an italics heading which I thought only for deleted docs
推荐答案
您共享的代码不会创建实际文档。它只是保留 root
中文档的ID,然后在其下创建一个带有实际<的 sub
集合code> doc11 document。
The code you shared doesn't create an actual document. It merely "reserves" the ID for a document in root
and then creates a sub
collection under it with an actual doc11
document.
在Firestore控制台中以斜体显示文档名称表示某个位置没有物理文档,但位置下有数据。当您删除以前存在的文档时,这是最典型的,但您的代码是完成相同操作的另一种方式。
Seeing a document name in italics in the Firestore console indicates that there is no physical document at a location, but that there is data under the location. This is most typical when you've deleted a document that previously existed, but your code is another way accomplishing the same.
无法在一个文档中创建两个文档虽然您可以在一个中创建多个文档,但请致电交易或批量写入如果您想要。
There is no way to create two documents in one call, although you can create multiple documents in a single transaction or batch write if you want.
这篇关于一次创建包含子集合的doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!