4

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

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Snake
  • 14,228
  • 27
  • 117
  • 250

1 Answers1

1

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • oh I see so If I do db.collection("root").get then it will not get me doc1 because it is not actual document, correct? I thought batch write is used to do creation in any order. Because obviously I can't create sub if I don't have doc1 first ( well I can like above but same behaviour would occur, no )? – Snake Mar 10 '18 at 21:49
  • 1
    Is there any example of how to achieve that using transactions? – Théo Champion Aug 27 '19 at 21:45