Snake
Snake

Reputation: 14658

Create doc with subcollection in one shot

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) 

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

Upvotes: 4

Views: 1757

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599131

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.

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.

Upvotes: 1

Related Questions