Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created April 18, 2012 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aheckmann/2416177 to your computer and use it in GitHub Desktop.
Save aheckmann/2416177 to your computer and use it in GitHub Desktop.
populated invitees
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();
});
});
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment