Search code examples
firebasegoogle-cloud-firestorebackupgcloud

Do Firestore imports merge or override collections?


Let's say I have the current Firestore data:

COLLECTION_1
  DOC_A
  DOC_B
  DOC_C
COLLECTION_2
  DOC_X

And I have some exported data, which is:

COLLECTION_1
  DOC_B        // DOC_A IS NOT HERE
  DOC_C
  DOC_D        // DOC_D IS AN "EXTRA"

// COLLECTION_2 IS MISSING

COLLECTION_3   // THERE IS AN "EXTRA" COLLECTION
  DOC_3

What happens when I import data data? Will some kind of "merge" occur, or my entire Firestore will be replaced with the imported data as is? I'm gessing DOC_D and COLLECTIO_3 will be added for sure. But will DOC_A and COLLECTION_2 (the ones missing on the exported data) still be there after the import?

PS: I'm asking this on the context of a full import (i.e: all collections).


To import specific collections, it seems that you need to use an export that exported specific collections, and you need to import it back using gcloud. The web console only allows for full imports. Anyway, I think the global "merge or override" behavior on a full import is probably similar to the same behavior over a specific collection.

Docs on import/export

enter image description here


Solution

  • This was super quick to test it. The result is

    COLLECTION_1
      DOC_A
      DOC_B
      DOC_C
      DOC_D
    COLLECTION_2
      DOC_X
    COLLECTION_3 
      DOC_3
    
    

    Everything is happening by document. Mentioned documentation says:

    If a document with the same ID already exists, the import overwrites the existing document.

    If a document in your database is not affected by an import, it will remain in your database after the import.

    And this exactly happening. Documents that are exist in the import are overridden, and not existing are added. If document is in Firestore but not in import it remains unchanged.

    BTW if you test it in GUI, you have to refresh the page after import. Otherwise changes are not visible (at least it happened to me).