我的团体收藏就像这样
[
{
"_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",
}
]
},
]
我想列出特定群组的群组管理员
为此我使用了以下查询
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",
}
]
我该怎么做,请建议
答案 0 :(得分:1)
如果您只是在寻找一条记录,则应使用 findOne()而不是 find()。然后访问该组的group_admins属性。即。
groupModel.findOne({_id:55fdbaa7457aa1b9bd7f7cf7},'group_admins -_id',function(err,groupDetails){
res.json(groupDetails['group_admins']);
});