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();
但是我沒有得到預期的結果
你需要使用$ elemMatch投影算。 – 2013-05-13 22:16:10
可以請你給我詳細,因爲我在這裏嘗試相同的db.messages.find({「對話」:{$ elemMatch:{「to._id」:ObjectId(「5048d2e5fbdac48208000042」)}}})。pretty() ;我沒有收到結果 – Elankeeran 2013-05-13 22:16:40
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