Chapter 2, quiz 2

In the second quiz option 3 is: “Embed all the fields for a city as a subdocument in the corresponding county document.”
I don’t understand what this means and how it is different from option 1: " * Embed the entities for the cities as an array of sub-documents in the corresponding county document."

Perhaps this is poorly worded and the intention was to pluralize city to cities? Is it asking to embed all the city information into a single sub document? Option 3 uses singular city and to me this implies that you would do this for each city, resulting in a list of subdocuments, one per city.

The main difference is that one is an array of cities. This can be thought of representing all the cities in a country. This allows you to grab all the cities when you find the country.cities field See the example below:

{
    country_name: "USA",
    cities: [
        {
            "city name": "New york",
            "population": 1111
        },
        {
            "city name": "Chicago",
            "population": 1111
        }
    ]
    
}

If you look at the other option it doesn’t work:

{
    country_name: "USA",
    "New York": {
            "city name": "New york",
            "population": 1111
        },
        "Chicago": {
            "city name": "Chicago",
            "population": 1111
        }
    
    
}

You can’t query all cities in the country, since each are it’s own “document”. Since we are doing a one to many you need to be able to pull the “many” from the "one.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.