0

Hi i am new to Mongodb... I have a collection i.e.

{
    "_id" : ObjectId("5b5437c1e8c56c92098f684f"),
    "name" : "Mongo",
    "list" : [
        {
            "id" : 1,
            "author" : "biswa"
        },
        {
            "id" : 2,
            "author" : "jack",
            "editions" : {
                "version" : 8
                "version" : 9
            }
        },
        {
            "id" : 3,
            "author" : "jony"
        }
    ]
}

Now i want find record whose id is 2 of list array..i tried db.doc.find({name : "Mongo"},{"list.id" : 2}).pretty() but it shows some different output i.e.

{
    "_id" : ObjectId("5b5437c1e8c56c92098f684f"),
    "list" : [
        {
            "id" : 1
        },
        {
            "id" : 2
        },
        {
            "id" : 3
        }
    ]
}

what will be the find command for this...

  • 1
    ***but it shows some erroneous output*** what's that? – Ashh Jul 23 '18 at 08:08
  • I mean different output which is not as per my requirement.... –  Jul 23 '18 at 08:10
  • Then please post the output you expect from above collection – Ashh Jul 23 '18 at 08:11
  • `{ "_id" : ObjectId("5b5437c1e8c56c92098f684f"), "name" : "Mongo", "list" : [ { "id" : 2, "author" : "jack", "editions" : { "version" : 8 "version" : 9 } } ] }` Like this.... –  Jul 23 '18 at 08:13
  • 1
    use `$elemMatch` projection https://stackoverflow.com/questions/50973336/mongodb-query-subdocuments-by-field-valeu/50973913#50973913 – Ashh Jul 23 '18 at 08:16
  • Thanks @Anthony I will try.... –  Jul 23 '18 at 08:22

0 Answers0