node.js - Selecting Subdocument array list only in mongoose -
my group collection goes this
[ { "_id": "55fdbaa7457aa1b9bd7f7cf7", "group_name":"tango charli", "group_desc":"this group 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 making love , chatting", "group_admins": [ { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "mike", }, { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "alice", } ] }, ]
i want list group admins of particular group
for using follwing query
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
it gives following output
[ { "group_admins": [ { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "james", }, { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "james", } ] } ]
but want output in following format
[ { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "james", }, { "_id": "55ff7fca8d3f6607114dc57d", "user_id": "55ff7fca8d3f7607114dc57e", "name": "james", } ]
how can , please suggest
if looking 1 record, should use findone() instead of find(). access group_admins property of that. i.e.
groupmodel.findone({_id:55fdbaa7457aa1b9bd7f7cf7},'group_admins -_id',function(err,groupdetails){ res.json(groupdetails['group_admins']); });
Comments
Post a Comment