Search code examples
firebaseangularfire2google-cloud-firestore

Get documents names in Firestore


When I get several documents from a collection, the result is only an array with each doc data.

firestore.collection("categories").valueChanges().subscribe(data => {
    console.log(data);
    // result will be: [{…}, {…}, {…}]
};

How can I get the name of each doc?

The ideal result would look like this:

{"docname1": {…}, "docname2": {…}, "docname3": {…}}

Solution

  • // Print each document 
    db.collection("categories")
        .onSnapshot((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                console.log(doc.data()); // For data inside doc
                console.log(doc.id); // For doc name
            }
        }