2013-05-13 37 views
0

我要過濾從我的查詢只庫馬爾「到」項目"_id" : ObjectId("5048d2e5fbdac48208000042")消息和預期的結果怎麼裏面濾鏡陣列MongoDB中

{ 
    "_id" : ObjectId("5191502a2f1b3ca33e000016"), 
    "message" : "<p>sssdasd<br></p>", 
    "subject" : "test message to nag", 
"date" : ISODate("2013-05-13T20:42:19.349Z") 
    } 

從下面的集合

{ 
     "__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 to nag", 
     "to" : { 
       "_id" : ObjectId("5048d2e5fbdac48208000042"), 
       "name" : "Kumar" 
     } 
} 

我試着用下面的查詢

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

但是我沒有得到預期的結果

+0

你需要使用$ elemMatch投影算。 – 2013-05-13 22:16:10

+0

可以請你給我詳細,因爲我在這裏嘗試相同的db.messages.find({「對話」:{$ elemMatch:{「to._id」:ObjectId(「5048d2e5fbdac48208000042」)}}})。pretty() ;我沒有收到結果 – Elankeeran 2013-05-13 22:16:40

+0

db.message.find({'conversation.to._id':ObjectId(「5048d2e5fbdac48208000042」)},{'subject':1,'conversation.message':1,對話:{$ elemMatch: {'to.name':「Kumar」}},'conversation.to':1})。pretty(); – 2013-05-14 01:13:15

回答

0

我已經使用了聚合框架我能夠得到預期的結果。

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"} 
         } 
        } 
    ); 

請分享如果有不同於上面