简体   繁体   中英

Get all collections with sub-collection in Firestore

I have a collection of countries and for each document of it, I have a collection of cities.

I want to get all countries and their cities like the array below. I am using NodeJs SDK.

[
    {
        "country": {
            "codeISO3166": "NL",
            "name": "Netherlands",
            "id": "47891a32-6eb3-4516-9ca1-b1ae16f9fcd3",
            "isEnable": true
        },
        "cities": [
            {
                "name": "Amsterdam",
                "id": "a2394abb-937c-4ba1-92cd-c2ffa7f8d3b2",
                "isEnable": true
            },
            {
                "name": "Utrecht",
                "id": "2dcae8be-4ef3-4f2f-a594-a0019b2c234a",
                "isEnable": true
            }
        ]
    },
    {
        "country": {
            "codeISO3166": "UK",
            "name": "United Kingdom",
            "id": "05236cab-dfdf-47ba-b7ed-b34fe6320183",
            "isEnable": true
        },
        "cities": [
            {
                "name": "London",
                "id": "a47a7c71-b0a2-4ba4-9170-2a5a9fed3757",
                "isEnable": true
            },
            {
                "name": "Liverpool",
                "id": "e6ac7fd7-aba3-45ab-8e13-e29f7dc35b04",
                "isEnable": true
            }
        ]
    }
]

There is no way to get a country and all its cities with one read operation.

Reads in Firestore are shallow, and only return documents from one collection or from all collections with a given name.

The closest you can get is:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM