public
Last active

populated invitees

  • Download Gist
invitees.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_invitees');
 
var userSchema = new Schema({
firstName: String
});
var U = mongoose.model('User', userSchema);
 
var eventMemberSchema = new Schema ({
user: { type : Schema.ObjectId, ref : 'User' },
});
var eventSchema = new Schema({
invitees : [eventMemberSchema]
});
 
 
var A = mongoose.model('A', eventSchema);
 
mongoose.connection.on('open', function () {
var u = new U({ firstName: 'aaron' });
u.save(function (err) {
if (err) throw err;
 
var a = new A;
a.invitees = [{ user: u._id }];
a.save(function (err, a) {
if (err) return console.error(err.stack||err);
 
A.find({ 'invitees._id': a.invitees[0]._id })
.populate('invitees.user')
.run(function (err, docs) {
if (err) console.error(err.stack||err);
console.error('populated user of first result', docs[0].invitees[0]);
 
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
});
})
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.