0
votes

I want to get all the documents from a collection, and then with them, get their id. Here how my collections is user collection It's only one collections with multiple documents. I try this but it doesn't work :

let userRef = admin.firestore().collection('users');
      return userRef.get().then(querySnapshot => {
        let docs = querySnapshot.docs;
        for (let doc of docs) {
           console.log(doc.id);
        }
        return true;
      });

UPDATE

What i was really trying to do, was to get all the documents id of a parent collections, so that i can use them to iterate in each of these documents which contain a subcollection.

So when i do the same thing who worked for the user collection up here, in this case with a parent collection which have documents id which contain a subcollection, it doesn't work. It's like my collection have no documents in it.

let savedRef = await admin.firestore().collection('saved');
        return savedRef.get().then(querySnapshot => {
          console.log(querySnapshot);
          let docs = querySnapshot.docs;
          for (let doc of docs) {
             console.log(doc.id);
          }
          return true;
        });

saved collection which contain documents with a subcollection

Do you have any idea why ? Thank you,

2
What does your code that is different than what you expect? It looks like it would work.Doug Stevenson
Yes it worked, but only when it's one collections with no nested subcollection. What I was really trying to do was to get all the documents id from the parent collection so that i can iterate in each subcollections in the document of the parent collection.Jeremy Dormevil

2 Answers

4
votes

Yes, querySnapshot can be easily iterated itself and get what you want. This is how I normally iterate Firestore query Snapshots:

//I like to separate DB instance for re-utilization
var db = admin.firestore()

//Also a good practice to separate reference instance
var usersReference = db.collection("users");

//Get them
usersReference.get().then((querySnapshot) => {

    //querySnapshot is "iteratable" itself
    querySnapshot.forEach((userDoc) => {

        //userDoc contains all metadata of Firestore object, such as reference and id
        console.log(userDoc.id)

        //If you want to get doc data
        var userDocData = userDoc.data()
        console.dir(userDocData)

    })

})
1
votes

In order to iterate through subcollection elements inside a specific document, you can do the following:

db.collection("ParentCollection").doc("DocumentID").collection("SubCollection").get()
.then((querySnapshot) => {
      ...
  });
});

If you want to iterate through all the Sub Collections of all the Parent Collections you can do the following:

db.collection("ParentCollection").get().then((querySnapshot) => {
  querySnapshot.forEach((document) => {
    document.ref.collection("SubCollection").get().then((querySnapshot) => {
      ...
    });
  });
});

EDIT: Adding the exact code sample that worked for me:

const app = express();
const Firestore = require('@google-cloud/firestore');

const db = new Firestore({
  projectId: 'my-project-id',
  keyFilename: '/path/to/service/account/key/file.json',
});

app.get('/', async (req, res) => {
  db.collection("ParentCollection").get().then((querySnapshot) => {
    // console.log(querySnapshot)
    querySnapshot.forEach((document) => {
      document.ref.collection("SubCollection").get().then((querySnapshot) => {
        console.log(querySnapshot)
        querySnapshot.forEach((document) => {
          console.log(document.id, '=>', document.data());
        });
      });
    });
  });
  res
  .status(200)
  .send('Hello, world!\n')
  .end();
});