1
 {
 _id:"101",
 title : "this is title",
 comments: 
 [{
    username : "mark zucker",
    comment: "awsome"
    }]

  }

i tried to get the comment with username "mark zucker" by this code below

 app.get('/all',function(req , res){
 db.collection.find({"_id":101},{"comments.username":'mark zucker'},function(err,docs){res.send(docs,{data:docs});});
 } );

but i get the output as

  {
  "data": [
    {
     "_id": 101,
     "comments": [
      {
        "user": "mark zucker"
      },
       {
        "user": "bella"
      }
    ]
    } 
 ]
  }

please help me to get only the particular comment with username :"mark zucker" .

bibin david
  • 1,905
  • 3
  • 12
  • 8
  • http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/ – Thilo May 16 '14 at 04:26
  • Or just basic projection using the [positional `$`](http://docs.mongodb.org/manual/reference/operator/projection/positional/) operator. `$elemMatch` should only be required when you require matching on more than one property of an array element. Both forms are demonstrated just a [few questions ago](http://stackoverflow.com/a/23692340/2313887) – Neil Lunn May 16 '14 at 04:29
  • if some one of you can explain it in answer portion i can approve it so it help me to close this question – bibin david May 16 '14 at 04:31
  • @NeilLunn yeah it works fine with $elemMatch thank you – bibin david May 16 '14 at 04:36
  • do u know how to update the document with _id:101 with comments username : mark zucker ? @NeilLunn – bibin david May 16 '14 at 05:01
  • Updating is a different question, but the hint is looking at the form of the query as used in the second example in the answer I gave you a link to. – Neil Lunn May 16 '14 at 05:10

0 Answers0