skylize
skylize

Reputation: 1421

How to list subcollections in a Cloud Firestore document

Say I have this minimal database stored in Cloud Firestore. How could I retrieve the names of subCollection1 and subCollection2?

rootCollection {
    aDocument: {
        someField: { value: 1 },
        anotherField: { value: 2 }
        subCollection1: ...,
        subCollection2: ...,
    }
}

I would expect to be able to just read the ids off of aDocument, but only the fields show up when I get() the document.

rootRef.doc('aDocument').get()
  .then(doc =>

    // only logs [ "someField", "anotherField" ], no collections
    console.log( Object.keys(doc.data()) )
  )

Upvotes: 21

Views: 16064

Answers (4)

Dan McGrath
Dan McGrath

Reputation: 42008

It is not currently supported to get a list of (sub)collections from Firestore in the client SDKs (Web, iOS, Android). From the documentation:

Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.

In server-side SDKs this functionality does exist. For example, in Node.js you'll be after the ListCollectionIds method:

var firestore = require('firestore.v1beta1');

var client = firestore.v1beta1({
  // optional auth parameters.
});

// Iterate over all elements.
var formattedParent = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");

client.listCollectionIds({parent: formattedParent}).then(function(responses) {
    var resources = responses[0];
    for (var i = 0; i < resources.length; ++i) {
        // doThingsWith(resources[i])
    }
})
.catch(function(err) {
    console.error(err);
});

Upvotes: 6

danday74
danday74

Reputation: 56936

This answer is in the docs

Sadly the docs aren't clear what you import.

Based on the docs, my code ended up looking like this:

import admin, { firestore } from 'firebase-admin'

let collections: string[] = null
const adminRef: firestore.DocumentReference<any> = admin.firestore().doc(path)
const collectionRefs: firestore.CollectionReference[] = await adminRef.listCollections()
collections = collectionRefs.map((collectionRef: firestore.CollectionReference) => collectionRef.id)

This is of course Node.js server side code. As per the docs, this cannot be done on the client.

Upvotes: 2

0xC0DEBA5E
0xC0DEBA5E

Reputation: 176

It seems like they have added a method called getCollections() to Node.js:

firestore.doc(`/myCollection/myDocument`).getCollections().then(collections => {
  for (let collection of collections) {
    console.log(`Found collection with id: ${collection.id}`);
  }
});

This example prints out all subcollections of the document at /myCollection/myDocument

Upvotes: 5

Ronnie Smith
Ronnie Smith

Reputation: 18555

Isn't this detailed in the documentation?

/**
 * Delete a collection, in batches of batchSize. Note that this does
 * not recursively delete subcollections of documents in the collection
 */
function deleteCollection(db, collectionRef, batchSize) {
    var query = collectionRef.orderBy('__name__').limit(batchSize);

    return new Promise(function(resolve, reject) {
        deleteQueryBatch(db, query, batchSize, resolve, reject);
    });
}

function deleteQueryBatch(db, query, batchSize, resolve, reject) {
    query.get()
        .then((snapshot) => {
            // When there are no documents left, we are done
            if (snapshot.size == 0) {
                return 0;
            }

            // Delete documents in a batch
            var batch = db.batch();
            snapshot.docs.forEach(function(doc) {
                batch.delete(doc.ref);
            });

            return batch.commit().then(function() {
                return snapshot.size;
            });
        }).then(function(numDeleted) {
            if (numDeleted <= batchSize) {
                resolve();
                return;
            }

            // Recurse on the next process tick, to avoid
            // exploding the stack.
            process.nextTick(function() {
                deleteQueryBatch(db, query, batchSize, resolve, reject);
            });
        })
        .catch(reject);
}

Upvotes: 0

Related Questions