如何在 firestroe 9 的子集合中为文档设置特定的查询参数?

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

如何在使用快照时为子集合设置特定的采石场? 我正在尝试按 createdAt 对子集合结果进行排序,限制为最后 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();
      };
    }
  }, []);

您可以使用 CollectionReference 构建一个 query(),如下所示:

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

})

检查文档中的 Listen to multiple documents in a collection