How to retrieve nodes under two collections from Firestore?

I have this structure:

- collection(A) // there is two collections in the Firestore in the top.
                   // But I only wrote one the collection because it gets longer
     - doc(918409234)
              -collection(B)
                   - doc(918409234)
                             object is stored here
                   - doc(213123133)
                             object is stored here
                   - doc(332222212)
                             object is stored here
                   - doc(029299292)
                             object is stored here
                   - doc(434352342)
                             object is stored here
     - doc(341234123)
              -collection(B)
                   - doc(918409234)
                   - doc(213123133)
                   - doc(332222212)
                   - doc(029299292)
                   - doc(434352342)
     - doc(341234123)
              -collection(B)
                   - doc(918409234)
                   - doc(213123133)
                   - doc(332222212)
                   - doc(029299292)
                   - doc(434352342)

So I want to retrieve nodes under collection B, but it does not seem to be working. What is wrong with my code?

When to post:

 function send() {
     let path = db.collection(A).doc(randomnumber).
     return path.collection(B).doc(randomnumber).set(object);
 }

When to fetch:

 function fetch() {
     return db.collection(A).get().then(value => {
        value.forEach((doc => {
            console.log(doc.id, '=>', doc.data());
        }));
        return value;
    }).catch(err => {
        console.log('error found' + err)
        return err;
    })
 }

let snapshot = await firestore.fetch();
console.log('Snapshot: ' + snapshot); // prints [Object, Object]
snapshot.forEach(child => { 
    let query = child.data(); 
    console.log('Each data: ' + query.B); // not printed in the console
});

But I cannot get the first documents. So I cannot go to the next step to fetch the next collection(in this case collection B). The fetch method is invoked but only printed as [Object, Object]. What is wrong with this code? I expect to fetch three nodes in the example above. Am I doing wrong? I want to fetch all docs which are assigned with a random number

1 answer

  • answered 2018-11-08 08:09 Alex Mamo

    How to retrieve nodes under two collections from Firestore?

    There is no way to achieve this. Firestore queries are shallow, they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. A single query may only use properties of documents in a single collection.

    So I want to retrieve nodes under collection B

    If you want to get all documents within your B collection, you need to know that document id (918409234) in order to create a correct reference and query the that subcollection accordingly. Without knowing it, you cannot get those documents. To solve this, you should store that random document id into a variable and use it in your reference or set the document id as a known id, let's say a user id.