3
votes

I have a firestore collection which has some documents inside it. The documents contains only collections not any fields. So when I try to get all documents inside the root collection i got the snapshot size as zero. Is there any way to get the documents which has no fields but has some collection inside it?

My firestore structure is

document structure

You can also see that the id of documents is shown in italics

code for retrieving data

  db.collection("transactions").get()
    .then(snapshot => { 
      console.log(snapshot.size); returns zero
    })
2
I asked the same question: stackoverflow.com/questions/47240469/…J. Doe
@J.Doe Not same question. You know the document names hence you can use getCollections method, i commented on your question. My question how can i get document names which has only collections inside it.suhail c
As far as I know there isn't an API for that. However the document ids are being shown in italics because the top level document has been removed, not because they only contain collections. These will still be visible in the Firebase UI but you won't be able to query for them which is why your snapshot length is zero.Chris Edgington
top level document has been removed?? I didn't get thatsuhail c
So for example if at some point you deleted the 'transactions' collections the delete wouldn't be recursive, so the documents underneath would still exist. This is also true if the docuement either has no fields or an empty collection. The Firebase UI displays them in italics to show they can't be queried.Chris Edgington

2 Answers

1
votes
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
votes

Adapted from 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);
    });