dalenguyen / firestore-import-export

An application that can help you to backup and restore from Cloud Firestore | Firebase
https://www.npmjs.com/package/firestore-export-import
401 stars 109 forks source link

Sub collections as nested object #1

Closed raullarosa closed 6 years ago

raullarosa commented 6 years ago

In an attempt to migrate data from Firestore to the RealtimeDB I was hoping the collections of my data would be placed inside of an object. It seems trivial to accomplish and maybe I'm the only one with this use case so I will be working on this. (First PR coming through so bear with me!)

dalenguyen commented 6 years ago

Hi @raullarosa. I'm not sure how many subcollections that you have, but there is a way to get subcollection if you have its name. You can put this line of code inside the query for the main collection to get the sub-collection. In this case, my sub-collection name is 'sub'.

db.collection(collectionName).doc(doc.id).collection('sub').get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
      })
})

This is a quick solution in case of you need it. I will come up with a better one later.

talpanika6 commented 6 years ago

how can i export a sub collection in a nested object? for example i have users collection, for export i will get all the users but without the sub collections of them. i want to export users/{userId}/purchase for example, how can i achieve that?

dalenguyen commented 6 years ago

Hi @talpanika6, I added the ability to export the sub-collection. You can check it now.