Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoose - Retrieving object from ref query
    primarykey
    data
    text
    <p>I've got the following schemas:</p> <pre><code>var userSchema = new Schema({ firstName: String, lastName: String, emailAddress: {type: String, set: toLower, index: {unique: true}}, }); var eventMemberSchema = new Schema ({ user: { type : Schema.ObjectId, ref : 'User' }, created: { type: Date, default: Date.now } }); var eventSchema = new Schema({ id : String, name : String, startDate : Date, endDate : Date, venue : { type : Schema.ObjectId, ref : 'Venue' }, invitees : [eventMemberSchema], }); </code></pre> <p>What I'm trying to do, is query the events, with an invitation._id, and ultimately get back the user...</p> <p>invitees->eventMember->user</p> <p>So far i've got:</p> <pre><code>Event .find({ "invitees._id": req.query.invitation_id }) .populate('user') .run(function (err, myEvent) { console.log('error: ' + err); console.log('event: '+ myEvent); }) </code></pre> <p>This works, and console shows the output of myEvent... (I realise I don't need the populate part of my mongoose query above for this... i'm just testing)</p> <p>I'm struggling on how to get, what I'd basically describe as: myEvent.invitees.user</p> <p><strong>EDIT</strong></p> <p>As an update... This <em>works</em> - however, it kind of sucks, as now i'll need to do another db operation to get the user (i realise ref in mongoose does this under the hood)</p> <pre><code>Event .findOne({ "invitees._id": "4f8eea01e2030fd11700006b"}, ['invitees.user'], function(err, evnt){ console.log('err: '+ err); console.log('user id: '+ evnt.invitees[0].user); //this shows the correct user id }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload