Firestore查询仅包含集合的文档

时间:2018-01-29 09:47:34

标签: javascript firebase google-cloud-firestore

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

我的firestore结构是

document structure

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

检索数据的代码

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

2 个答案:

答案 0 :(得分:1)

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 :(得分:0)

改编自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() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });