Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get all documents in a subcollection (Firestore)

I'm having difficulties querying all documents inside a subcollection in my Firestore database. (I am using Node.js)

My goal is to get all the data from every document inside the subcollection named favorites

My database structure looks like this. firestore db stucture

I looked at the documentation at https://firebase.google.com/docs/firestore/query-data/get-data but without any result how to solve the problem.

My query looks like this right now:

exports.getAllFavoritePodcasts = (req, res) => {
  db.collection('users')
    .doc(req.user.userId)
    .collection('favorites')
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
}

But I get TypeError: db.collection(...).doc(...).collection(...).then is not a function

like image 600
Jonas Alvarson Avatar asked Nov 01 '19 09:11

Jonas Alvarson


1 Answers

With Firebase Version 9 (Jan, 2022 Update):

If subcollection(subcoll) contains 3 documents(doc1, doc2, doc3) like below:

coll > doc > subcoll > doc1 > field1: "value1", field2: "value2"
                       doc2 > field1: "value1", field2: "value2"
                       doc3 > field1: "v1",     field2: "v2"

You can get all 3 documents(doc1, doc2, doc3) of subcollection(subcoll) with the code below:

import { getDocs, collection } from "firebase/firestore";

const docsSnap = await getDocs(collection(db,"coll/doc/subcoll"));
      
docsSnap.forEach((doc) => {
  console.log(doc.data()); // "doc1", "doc2" and "doc3"
});

Without forEach() method to get all 3 documents(doc1, doc2, doc3) of subcollection(subcoll) below:

import { getDocs, collection } from "firebase/firestore";

const docsSnap = await getDocs(collection(db, "coll/doc/subcoll"));
        
console.log(docsSnap.docs[0].data()); // "doc1"
console.log(docsSnap.docs[1].data()); // "doc2"
console.log(docsSnap.docs[2].data()); // "doc3"

In addition, you can get 2 documents(doc1, doc2) of subcollection(subcoll) with while below:

import { query, collection, where, getDocs } from "firebase/firestore";

const q = query(collection(db, "coll/doc/subcoll"), 
                where("field1", "==", "value1"));
    
const docsSnap = await getDocs(q);

docsSnap.forEach((doc) => {
  console.log(doc.data()); // "doc1" and "doc2"
});

Without forEach() method to get 2 documents(doc1, doc2) of subcollection(subcoll) with while below:

import { query, collection, where, getDocs } from "firebase/firestore";

const q = query(collection(db, "coll/doc/subcoll"),
                where("field1", "==", "value1"));
    
const docsSnap = await getDocs(q);
    
console.log(docsSnap.docs[0].data()); // "doc1"
console.log(docsSnap.docs[1].data()); // "doc2"

Again, in addition, you can get only one specific document(doc3) of subcollection(subcoll) with the code below:

import { getDoc, doc } from "firebase/firestore";

const docSnap = await getDoc(doc(db, "coll/doc/subcoll/doc3"));
          
console.log(docSnap.data()); // "doc3"
like image 94
Kai - Kazuya Ito Avatar answered Oct 15 '22 17:10

Kai - Kazuya Ito