I am using mongodb 3.4.0 in MacOS Sierra 10.12.2
my collection is below:
{
"_id" : ObjectId("58837a559caf2fc968adc64d"),
"box_location" : [
{
"country" : "Taiwan",
"country_code" : "TW",
"location" : [
{
"city" : "Taipei",
"name" : "Taipei 101"
}
]
},
{
"country" : "Hong Kong",
"country_code" : "HK",
"location" : [
{
"city" : "Hong Kong",
"name" : "Hung Hom Station"
}
]
}
]
}
I have use the following queries
db.setting.findOne({"box_location.country_code":"TW"}, {box_location:1, _id:0})
db.setting.find({"box_location.country_code":"TW"}, {box_location:1, _id:0})
db.setting.find({
"box_location": {
$elemMatch :{
"country_code":"TW"
}
}
})
and always return all document in box_location instead of just box_location in TW country code.
I have been searching solution here and it always mention dot notation or elemMatch but none of them works
When I query country code TW, it should return only
"box_location" : [
{
"country" : "Taiwan",
"country_code" : "TW",
"location" : [
{
"city" : "Taipei",
"name" : "Taipei 101"
}
]
}
]