Create a gist now

Instantly share code, notes, and snippets.

populating a referenced mongoose model, three levels deep
var mongoose = require('mongoose'),
assert = require('assert')
var Schema = mongoose.Schema;
var ContainerSchema = new Schema({
name: String,
var MasterSchema = new Schema({
name: String,
container: [{type: Schema.Types.ObjectId, ref: 'Container'}]
var PanelSchema = new Schema({
name: String,
master: [MasterSchema]
var AppSchema = new Schema({
name: String,
panel: [PanelSchema]
var App = mongoose.model('App', AppSchema);
var Panel = mongoose.model('Panel', PanelSchema);
var Master = mongoose.model('Master', MasterSchema);
var Container = mongoose.model('Container', ContainerSchema);
var container = new Container({name:'page'}); {
var master = new Master({name:'meester'});
master.container.push(container); {
var panel = new Panel({name:'paneel'});
panel.master.push(master); {
var app = new App({name:'application'});
app.panel.push(panel); {
.exec(function(err, prog) {
if (err) throw new Error('not found');
assert.equal(prog.panel[0].name, 'paneel');
assert.equal(prog.panel[0].master[0].name, 'meester');
assert.equal(prog.panel[0].master[0].container[0].name, 'page'); // <== fails

contains the id, thus [ 50923746587365835 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment