一次性创建包含子集合的文档 [英] 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,然后在其下创建一个带有实际 doc11
文档的 sub
集合.
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.
无法在一次调用中创建两个文档,尽管您可以在一个 transaction 或 批量写入.
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.
这篇关于一次性创建包含子集合的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!