1
我集團集是這樣的選擇子文檔數組列表僅在貓鼬
[
{
"_id": "55fdbaa7457aa1b9bd7f7cf7",
"group_name":"Tango Charli",
"group_desc":"This group is for chatting",
"group_admins": [
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
},
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
}
]
},
{
"_id": "55fdbaa7457aa1b9bd7f8de8",
"group_name":"Love zone",
"group_desc":"This group is for making love and chatting",
"group_admins": [
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "Mike",
},
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "alice",
}
]
},
]
我想列出特定羣體
的組管理員對於這個我使用follwing查詢
groupModel.find({_id:55fdbaa7457aa1b9bd7f7cf7},'group_admins -_id',function(err,groupDetails){
if(err)
{
res.json({
"isError":true,
"error":{
"status":1042,
"message":err
}
});
}
else
{
res.json(groupDetails);
}
}
);//end of groupModel find
它給出以下輸出
[
{
"group_admins": [
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
},
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
}
]
}
]
,但我想在下面的格式
[
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
},
{
"_id": "55ff7fca8d3f6607114dc57d",
"user_id": "55ff7fca8d3f7607114dc57e",
"name": "James",
}
]
輸出我怎麼能做到這一點,請建議
你好chriskely,感謝您迴應,指着我正確的方式....你的答案未經編輯res.json(groupDetails.shift()[ 'group_admins']);正在工作和生產結果,因爲我想.... 這個答案res.json(groupDetails ['group_admins']);不起作用......但我修改它爲res.json(groupDetails [0] ['group_admins']);現在工作完美..... 再次謝謝你 – Jorin
@Jorin:你在誤讀我的編輯。您需要使用findOne()而不是find()。我編輯的原因是因爲findOne是這個工作的正確工具,因爲你用id選擇你只想要返回一個結果。 – chriskelly
噢..對不起,您已經使用findOne(),....... ya,for findOne()..res.json(groupDetails ['group_admins']);正在工作和查找()... res.json(groupDetails [0] ['group_admins']); ..........再次感謝你... – Jorin