my collection is like this:
{
"_id" : ObjectId("590886af47627f86e5857141"),
"code" : "MY",
"pre_title" : "republic of",
"name" : "Malaysia",
"topics" : [
{
"name" : "overview",
"body" : "ergergerg123234",
"type" : "info",
"_id" : ObjectId("5931243d26fca3496dbffe92"),
"facts" : [
{
"key" : "capital",
"value" : "Kuala Lumpur",
"_id" : ObjectId("5931243d26fca3496dbffe96")
},
{
"key" : "population",
"value" : "232424234",
"_id" : ObjectId("5931243d26fca3496dbffe95")
},
{
"key" : "reliogion",
"value" : "Islam",
"_id" : ObjectId("5931243d26fca3496dbffe94")
},
{
"key" : "government_form",
"value" : "Federal constitutional monarchy",
"_id" : ObjectId("5931243d26fca3496dbffe93")
}
]
},
{
"name" : "Good to know",
"body" : "here in good to know",
"type" : "info",
"_id" : ObjectId("5931243d26fca3496dbffe90"),
"facts" : [
{
"key" : "key1",
"value" : "value1",
"_id" : ObjectId("5931243d26fca3496dbffe91")
}
]
}
]
}
I need to get access in topic where topicId ="X" and countryId =_id .I write this lines but result is wrong: so it have two condition. first countryId should match then TopicId
var query = {"_id":_id,"topics.$._id": topicId }
Country.findOne(query ,function (err, topic) {
console.log('topic',topic);
})
UPDATE:
it is working like this but also I need to go inside facts where key={XX}
Country.findById(query).select({ topics: { $elemMatch: { _id: topicId }
} }).
.then((topic,err) => {