i have such schema in mongoDB
{
"key":"ru",
"regions":[
{
"name":"moskovskaya",
"cities":[
{
"name":"moskva"
},
{
"name":"tula"
}
]
},
{
"name":"piterskaya",
"cities":[
{
"name":"piter"
},
{
"name":"luga"
}
]
}
]
}
i have some documents of such schema for different countries, how can i get an array of ALL cities from each document of this schema?
This is a straightforward query. The distinct()
method will beautifully get the job done.
db.collection.distinct("regions.cities.name")
which produces:
[ "luga", "moskva", "piter", "tula" ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With