0

Current Code

// items is an array.
// Array [
    Object {
      "id": "KQJfb2RkT",
      "name": "first",
    },
    Object {
      "id": "1mvshyh9H",
      "name": "second",
    },
  ]

storeSale = async ({ items }) => {
  this.salesCollection.add({
    status: 1,
    created_at: new Date(),
    updated_at: new Date(),
  });
};

When adding a document in SalesCollection, I want to add items as subcollection to this document.

I would appreciate it if you could give me any advices.

I would like to save like this. enter image description here

LPFJ
  • 187
  • 1
  • 4
  • 13

1 Answers1

2

You can use a batched write, as follows:

// Get a new write batch
let batch = db.batch();

// Set the value of parent
const parentDocRef = db.collection("parentColl").doc();
batch.set(parentDocRef, {
    status: 1,
    created_at: new Date(),
    updated_at: new Date(),
  });

//Set the value of a sub-collection doc

const parentDocId = parentDocRef.id;

const subCollectionDocRef = db.collection("parentColl").doc(parentDocId).collection("subColl").doc();
batch.set(subCollectionDocRef, {
    ...
  });

// Commit the batch
await batch.commit();

One key point to note: Actually, from a technical perspective, a parent collection and the sub-collections of the documents in this parent collection are not at all relating to each other.

Let's take an example: Imagine a doc1 document under the col1 collection

col1/doc1/

and another one subDoc1 under the subCol1 (sub-)collection

col1/doc1/subCol1/subDoc1

These two documents (and the two immediate parent collections, i.e. col1 and subCol1) just share a part of their path but nothing else.

One side effect of this is that if you delete a document, its sub-collection(s) still exist.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thank you so much. It worked. But I don't what to write in `batch.commit().then(() => { // ... });` I would appreciate it if you could tell me about it. – LPFJ May 08 '20 at 02:35
  • Very good answer. Thanks! Is there the chance to create a sub collection also in the context of a transaction? See also https://stackoverflow.com/questions/74575823/perform-fetch-request-within-a-firestore-transaction-receiving-cannot-modify-a. – Lorenzo B Nov 25 '22 at 17:08
  • @LorenzoB Thanks for your appreciation. Do not hesitate to upvote the answer ;-) I see in the comments of the other question that Doug Stevenson has answered and I can just confirm what he said: Don't use axios' calls in a Transaction. I would add "don't use forEach together with async" (see https://stackoverflow.com/search?q=user%3A3371862+forEach+async). So you probably have to totally reconsider your approach. – Renaud Tarnec Nov 25 '22 at 18:07
  • "Is there the chance to create a sub collection also in the context of a transaction? " Sure, as explained in the answer a subcollection is nothing more than a collection with a path containing 3 or more elements (always an odd value). Of course, the subcollection is only created when the first doc in the subcol is created. – Renaud Tarnec Nov 25 '22 at 18:13
  • @RenaudTarnec Thanks for your help. I've put my reply on my question. Now it works correctly. – Lorenzo B Nov 26 '22 at 15:28