使用Firestore查询仅包含集合的文档

4

我有一个Firestore集合,其中包含一些文档。这些文档仅包含集合而不包含任何字段。因此,当我尝试获取根集合内的所有文档时,快照大小为零。有没有办法获取那些没有字段但内部有一些集合的文档?

我的Firestore结构如下:

document structure

你可以看到文档的id是以斜体显示的。
检索数据的代码。
  db.collection("transactions").get()
    .then(snapshot => { 
      console.log(snapshot.size); returns zero
    })

我提出了同样的问题:https://stackoverflow.com/questions/47240469/get-collection-ids-that-are-inside-a-document-in-google-firestore - J. Doe
@J.Doe 不是同一个问题。你知道文档名称,因此可以使用 getCollections 方法,我在你的问题上发表了评论。我的问题是如何获取仅包含集合的文档名称。 - suhail c
1
据我所知,目前还没有相关的API。然而,文档ID以斜体显示是因为顶层文档已被删除,而不是因为它们只包含集合。这些文档仍将在Firebase UI中可见,但您无法查询它们,这就是为什么您的快照长度为零的原因。 - Chris Edgington
顶层文档已被删除?我没明白。 - suhail c
例如,如果您在某个时候删除了“交易”集合,则删除不会是递归的,因此下面的文档仍将存在。 如果文档没有字段或为空集合,则也是如此。 Firebase UI以斜体显示它们,以显示它们无法查询。 - Chris Edgington
我没有做过那样的事情。我是通过设置db.collection("transaction").doc(date).collection(collection_id).doc(doc_id).set({ ...values })创建了一个文档,最初它不存在。无论如何,我得到了回复,如果您的文档不包含数据,只指向其他子集合,那么该文档实际上并不存在。 - suhail c
2个回答

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.

0

源自使用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);
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接