Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all of the collection ids from document on Firestore? [duplicate]

I connect the database from React Native project.

In my Firestore database Buildings/Taipei, i want to get all of the collection ids like 201 202 203 enter image description here

I try to use the code:

const db = firebaseApp.firestore();

    db.collection('Buildings').doc('Taipei')
    .get()
    .then(doc => {
      console.log('get doc is =>');
      console.log(doc);
    }).catch(error => {
      console.log(`error is ${error}`);
    });

I can't see any collection id in the doc. enter image description here

Any help would be appreciated. Thanks in advance.

like image 675
Morton Avatar asked Sep 04 '18 10:09

Morton


People also ask

How do I get all the collection names 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 find my collection reference on firestore?

CollectionReference collectionReference = FirebaseFirestore. getInstance(). collection("public_messages"); ArrayList<String> ids = new ArrayList<>(); //Contains your keys ArrayList<Task<QuerySnapshot>> tasks = new ArrayList<>(); for (String id : ids) { Task<QuerySnapshot> task = collectionReference.


1 Answers

Unfortunately what you want to do is not currently possible in the mobile/web client.

The getCollections() method of the Cloud Firestore server client libraries lists all subcollections of a document reference.

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.

Source: List Subcollections of Document

like image 196
Philip Avatar answered Oct 14 '22 15:10

Philip