首页 文章

Firestore查询仅包含集合的文档

提问于
浏览
2

我有一个firestore集合,里面有一些文档 . 文档仅包含集合而不包含任何字段 . 因此,当我尝试获取根集合中的所有文档时,我将快照大小设置为零 . 有没有办法获得没有字段但内部有一些集合的文件?

我的火堆结构是

document structure

您还可以看到文档的ID以斜体显示

用于检索数据的代码

db.collection("transactions").get()
    .then(snapshot => { 
      console.log(snapshot.size); returns zero
    })

2 回答

  • 0
    let date = '2017-12-20' //date to be set in doc
    
    db.collection("transactions").doc(date).set({
      //set an empty object for each doc in transaction before your method 
      //of setting data 
      //This will create a field for each doc in transaction
    })
    .then(()=>{
      //your method of setting data here.
    })
    //Your query method here.
    //Now you will be able to query the docs in collection - transaction.
    
  • 1

    改编自Get Data with Cloud Firestore

    db.collection(“transactions”) . doc(“2018-01-02”) . collection(“education-1”) . get()
    .then(function(querySnapshot){
    querySnapshot.forEach(function(doc){
    // doc.data()永远不会为查询文档快照定义
    console.log(doc.id,“=>”,doc.data());
    });
    })
    .catch(function(error){
    console.log(“获取文件时出错:”,错误);
    });

相关问题