Search code examples
firebasegoogle-cloud-firestore

Return document collection as json from firestore


How can I return a collection of documents from firestore database. For example, I have a collection named countries in cloud firestore, and I want to fetch all the country names and ids as json , like this

[
    {
        "countryId" : 1,
        "countryName" : "Any"
    },
    {
        "countryId" : 2,
        "countryName" : "India"
    }
]

Now I am doing a foreach on the snapshot. Is that the only way to convert a collection to json?


Solution

  • What you're doing is correct. You have to iterate each document in the collection/query, and generate the JSON yourself from there. There is no single operation for converting a collection to some other format in its entirety.