Brett DeWoody
Brett DeWoody

Reputation: 62743

Set a collection as a sub-collection

I'm wondering if there's a more straightforward way to set a collection as a sub-collection in a new document. My current method is:

  1. Get CollectionA, which will be used as the sub-collection
  2. Create a ref to new DocA
  3. Set some data into DocA
  4. Create a ref to new CollectionB in DocA
  5. Use a batch() and loop through CollectionA, using batch(set, CollectionA.doc.data()) on each doc of CollectionA
  6. batch.commit() to commit all the docs from CollectionA into CollectionB

Is there a simpler way to accomplish this?

Like:

DocA.collection('CollectionB').set(CollectionA)

I've tried a few methods similar to this but CollectionA ends up living in a single doc within CollectionB. Instead of all the docs within CollectionA becoming docs within CollectionB.

Upvotes: 0

Views: 348

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

There is no API to duplicate documents from one collection into another. You will have to read the documents to a client, and then write them into the target collection.

I don't think there's any faster way to do that then by copying them in batches. Note that a batch can contain no more than 500 write operations, so you may have to perform multiple.

Upvotes: 1

Related Questions