0
我不認爲這是重複的,但請糾正我,如果我錯了。無論如何,我只想返回「apple」和與用戶的對象電子郵件地址相匹配的客戶成員角色。我使用$ elemMatch,但返回整個客戶對象,我只想要「成員」屬性,就是這樣。Mongodb - 查詢對象數組,但只返回一個屬性
{
"_id" : ObjectId("54d24e5df2878d40192beabd"),
"apple" : "yes",
"orange" : "yes",
"customers" : [
{
"name" : "Jay Smith",
"email" : "[email protected]",
"member" : "silver",
},
{
"name" : "Sarah Carter",
"email" : "[email protected]",
"member" : "gold",
},
{
"name" : "Jack Whatever",
"email" : "[email protected]",
"member" : "gold",
},
]
}
理想的結果回來是:
{
"_id" : ObjectId("54d24e5df2878d40192beabd"),
"apple" : "yes",
"member" : "gold"
}
甚至這樣就足夠了:
{
"_id" : ObjectId("54d24e5df2878d40192beabd"),
"apple" : "yes",
"orange" : "yes",
"customers" : [
{"member" : "gold"}
]
}
這是目前我有:
ItemsModel.find({ _id: { $in: _.pluck(user.items, 'itemId') }, active: true},
{apple: 1, customers: {$elemMatch: {email: user.email}} },
function(error, items) {
if (error) { return next(error); }
req.payload = {};
req.payload.items = items;
next();
});
任何幫助真的很感激。這甚至有可能嗎?謝謝!
我得到它的客戶:「成員」:「黃金」],而不是作爲「會員」:「金」,但我還是會紀念這是正確的答案。天才的東西。謝謝! – Justin
糟糕,應該是會員:'$ customers.member'。我修改了我的答案 –