node.js - Returning only first element of a sub array with $elemMatch -
I am using nodes and Mongoos, and there is a schema that looks like this:
< Pre> Var Subscriber schema = new schema ({'user': [{type: mojo sama.a type.object, ref: 'user'}], 'level': {type: string, enum: [' Swami ',' Sub ',' commenter ',' poster ']}' date ': {type: date, default: date.So}}); // group schema var group schema = new schema ({'group' ': [{type: mongosse.sema.tem.object, reference:' user '}],' groupname ': string,' subscriber ': [subscriberskema] });
I want to query the group to find all the groups where a user ( req.user._id
is stored through token authentication) Returns a single member array element with a customer (i.e. their _id
Consumers
array), and only with their _id
I 've read the Mongo documentation, because it seems what I want, and has created the question below. It gives the information that I want but returns all elements of the customers
array customers req.user._id
. > How can I return only one element of the ARM?
The current query, Subscriber
:
group of all elements. Find ({"subscribers.user": req.user._id}, {Subscriber: {$ elem match: {user: rc_id}}}) .sort ('groupName'). Populate ('groupOwner', 'email firstname lastname'). Populate ('subscribers.user', 'email firstname lastname') .exec (function (error, data) {if (error) {logger.error ('Unable to retrieve group for user:' + err.message) ; Res.status (500)} other {res.json (data);}});
This gives the following to the customer (via util.inspect (data [0]. Member)
):
< Code> subscriber [user: [[_id: 1234, email: 'm @ here.com', first name: 'test', last name: 'testals'}}} user: [[_id: 5678, email:' you Based on the @ there.com $ Elemmatch docs, I would agree that I would only see 1234 users because that record matchesreq.user._id
. Am I doing wrong here?Thank you!
In your projection parameter, use it:
{" User. $ ": 1}
This will return a group with only one object in its 'Customer' array
Comments
Post a Comment