13

I would automate the backup process of a firestore database.

The idea is to loop over the root document to build a JSON tree, but I didn't find a way to get all collections available for a document. I guess it's possible as in firestore console we can see the tree.

Any ideas?

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
eskan
  • 309
  • 1
  • 3
  • 13

5 Answers5

12

Its possible on web (client side js)

db.collection('FirstCollection/' + id + '/DocSubCollectionName').get().then((subCollectionSnapshot) => {
    subCollectionSnapshot.forEach((subDoc) => {
        console.log(subDoc.data());
    });
});

Thanks to @marcogramy comment

Daniel
  • 36,833
  • 10
  • 119
  • 200
11
firebase.initializeApp(config);

const db = firebase.firestore();

db.settings({timestampsInSnapshots: true});

const collection = db.collection('user_dat');

collection.get().then(snapshot => {

  snapshot.forEach(doc => {

    console.log( doc.data().name );    
    console.log( doc.data().mail );

  });

});
Ryosuke Hujisawa
  • 2,682
  • 1
  • 17
  • 18
8

Update
API has been updated, now function is .listCollections() https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#listCollections


getCollections() method is available for NodeJS.

Sample code:

    db.collection("Collection").doc("Document").getCollections().then((querySnapshot) => {
    querySnapshot.forEach((collection) => {
        console.log("collection: " + collection.id);
        });
    });
Mike Yang
  • 2,581
  • 3
  • 24
  • 27
  • 1
    API has been updated, now function is `.listCollections()`, https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#listCollections . Thanks to https://stackoverflow.com/a/57248728/2162226. This got me going in right direction, Thanks – Gene Bo Aug 16 '19 at 00:13
  • 1
    I cannot find this for Flutter – Dani Nov 25 '19 at 16:29
6

If you are using the Node.js server SDK you can use the getCollections() method on DocumentReference: https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections

This method will return a promise for an array of CollectionReference objects which you can use to access the documents within the collections.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124
  • 1
    I can't find anything similar on the Web side (JavaScript in browser). Is it just me, or? – Leo Nov 16 '17 at 00:23
  • 2
    That's correct, this is only implemented in the server-side SDKs for now. – Sam Stern Nov 16 '17 at 23:07
  • 1
    I just noticed that it is possible to run code on the server side. This will run on Node.js. Would it be possible to use this now to call getCollections() and output the result to the client? – Leo Dec 11 '17 at 18:42
  • 3
    Web-side I've have implemented the following workaround: db.collection(firstCollections + '/' + docId + '/' + subCollection).get() for obtaining documents in 'subCollection' – marcogramy Feb 16 '18 at 11:22
  • @SamStern Is there a plan to add this feature to the client side SDKs? It's really inconvenient and potentially expensive to have to use a cloud function to simply traverse the document tree. Major drawback if switching from Firebase, almost a non-starter – Patrick Goley Feb 27 '19 at 19:33
1

As mentioned by others, on the server side you can use getCollections(). To get all the root-level collections, use it on the db like so:

const serviceAccount = require('service-accout.json');
const databaseURL = 'https://your-firebase-url-here';
const admin = require("firebase-admin");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: databaseURL
});
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
db.getCollections().then((snap) => {
    snap.forEach((collection) => {
        console.log(`paths for colletions: ${collection._referencePath.segments}`);
    });
});
John Cummings
  • 1,949
  • 3
  • 22
  • 38