mongoose - how filter inside array in mongodb -


i want filter kumar "to" items"_id" : objectid("5048d2e5fbdac48208000042") message query , expected result

    {     "_id" : objectid("5191502a2f1b3ca33e000016"),     "message" : "<p>sssdasd<br></p>",      "subject" : "test message nag", "date" : isodate("2013-05-13t20:42:19.349z")     } 

from below collections

{         "__v" : 0,         "_id" : objectid("5191502a2f1b3ca33e000016"),         "conversation" : [                 {                         "date" : isodate("2013-05-13t20:42:19.349z"),                         "message" : "<p>sssdasd<br></p>",                         "_id" : objectid("5191502a2f1b3ca33e000017"),                         "to" : {                                 "_id" : objectid("5048d2e5fbdac48208000042"),                                 "name" : "kumar"                         },                         "from" : {                                 "_id" : objectid("503fdbfa294f6db74de649ea"),                                 "name" : "anand"                         }                 },                 {                         "message" : "<p>reply</p>",                         "date" : isodate("2013-05-13t21:05:33.453z"),                         "_id" : objectid("5191559c7d2c386741000018"),                         "to" : {                                 "_id" : objectid("503fdbfa294f6db74de649ea"),                                 "name" : "anand"                         },                         "from" : {                                 "_id" : objectid("5048d2e5fbdac48208000042"),                                 "name" : "kumar"                         }                 },                 {                         "message" : "<p>reply2<br></p>",                         "date" : isodate("2013-05-13t21:05:55.006z"),                         "_id" : objectid("519155b1ca98b66641000014"),                         "to" : {                                 "_id" : objectid("503fdbfa294f6db74de649ea"),                                 "name" : "anand"                         },                         "from" : {                                 "_id" : objectid("503fdbfa294f6db74de649ea"),                                 "name" : "anand"                         }                 }         ],         "from" : {                 "_id" : objectid("503fdbfa294f6db74de649ea"),                 "name" : "anand"         },         "sent" : isodate("2013-05-13t20:42:19.349z"),         "subject" : "test message nag",         "to" : {                 "_id" : objectid("5048d2e5fbdac48208000042"),                 "name" : "kumar"         } } 

i try below query

 db.messages.find({'conversation.to._id':objectid("5048d2e5fbdac48208000042")},{'subject':-1, 'conversation.message': 1,'conversation.to':1}).pretty(); 

but not expected result

i have used aggregate framework able expected result.

 db.message.aggregate(                     { $unwind : "$conversation" },                     { $match: {                                      "conversation.to.name" : "anand"                                     }                     },                     {                       $sort: {                                     "conversation.date" : -1                        }                     },                     {                       $group: {                                     _id:"$_id",                                     msgsubject: {$first:"$subject"},                                     msgdate: {$first:"$conversation.date"},                                     latestmsg: {$first:"$conversation.message"}                       }                     }     ); 

please share if different above


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -