Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching all collections in Firestore

Tags:

I am using Firestore in my project, so i want to show all collection names in my project.

Is it posible to get all collection names from Firebase project.

Thanks..

like image 286
Raju Chittampalle Avatar asked Jan 15 '18 07:01

Raju Chittampalle


People also ask

How do you get all the collections in firestore?

js admin clients have listCollections() on Firestore to get that list. Or, if you're looking for subcollections nested under a document, use DocumentReference. listCollections(). If you want to get a list on any platform, you should maintain that list yourself in a known collection inside a known document id.

How do I read a collection in Firebase?

You can read from a collection by querying it directly via the collection name. There are four different types of methods that you can use to keep track of any changes that may occur against your Firebase data. This will return a list of data. There is no document metadata and makes it simple to render to a view.


2 Answers

There are limited public APIs to get a list of top-level collections. Mobile clients (Android, iOS, web) have no API. Node.js admin clients have listCollections() on Firestore to get that list. Or, if you're looking for subcollections nested under a document, use DocumentReference.listCollections().

If you want to get a list on any platform, you should maintain that list yourself in a known collection inside a known document id.

like image 63
Doug Stevenson Avatar answered Sep 28 '22 05:09

Doug Stevenson


You can do the following:

const admin = require("firebase-admin"); const db = admin.firestore();  db.listCollections()   .then(snapshot=>{       snapshot.forEach(snaps => {         console.log(snaps["_queryOptions"].collectionId); // LIST OF ALL COLLECTIONS       })   })   .catch(error => console.error(error));  

This works with NodeJS.
Remember that you will need to download firebase-admin and do the necessary configuration for Firebase server side.

like image 21
Joel Nasibu Avatar answered Sep 28 '22 05:09

Joel Nasibu