0

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) => {
Mehrnoosh
  • 879
  • 2
  • 12
  • 27
  • `Country.findOne({ "_id": _id, "topics._id": topicId },{ "topics.$.": 1 },function(err,topic) {`. Is the correction to your syntax. Read the linked answers and the documentation more carefully. – Neil Lunn Jun 05 '17 at 11:49
  • I need another where also : { "_id": _id, "topics._id": topicId,"topics.$.facts.key":_key } . actually where clause is 3 condition – Mehrnoosh Jun 06 '17 at 03:40
  • Read the [linked answers](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection). Everything is covered in there. There is just about every possible case for matching and "filtering" singular or multiple array elements. You also mainly are still not understanding the basic syntax. So again read and learn. And when you have a new question that differs from the one you actually asked, then you [Ask a new Question](https://stackoverflow.com/questions/ask). But please understand the answers given already first. – Neil Lunn Jun 06 '17 at 03:46
  • @NeilLunn: I read it completely . my problem is different .this one just have 1 inner array "shape". mine have another one facts.my first problem solved by your link. but now I need to go inside facts so query is like this: `var query = { "_id": _id, "topics._id": topicId, "facts": {$elemMatch: {key: key} } }` however it is not working also. – Mehrnoosh Jun 06 '17 at 04:05

1 Answers1

0
var query = {"_id":_id,"topics._id": topicId }
 Country.findOne(query,{topics.$:1} ,function (err, topic) {

   console.log('topic',topic);
})

remove $ from query use topics._id instead topics.$._id

Azeem Malik
  • 490
  • 3
  • 16