Home > Net >  How to set specific query parameters for docs in a sub collection in firestroe 9?
How to set specific query parameters for docs in a sub collection in firestroe 9?

Time:04-09

How to set a specific quarry for a sub collection when using a snapshot ? I am tring to sort sub collection resutls by createdAt with the limit of last 5

 useEffect(() => {
      //const querydata = query(docRefComm, orderBy('createdAt'), limit(5));
      let collectionRef = collection(db, 'Comm', docId, 'messages');
      const unsub = onSnapshot(collectionRef , (doc) => {
       doc.forEach((el) => {
          console.log(el.data());
        });
      });
      return () => {
        unsub();
      };
    }
  }, []);

CodePudding user response:

You can build a query() using CollectionReference as shown below:

let collectionRef = collection(db, 'Comm', docId, 'messages');
const q = query(collectionRef, orderBy('createdAt'), limit(5))
      
const unsub = onSnapshot(q , (qSnap) => { 

})

Checkout Listen to multiple documents in a collection in the documentation.

  • Related