Last active
December 21, 2015 21:28
-
-
Save donnut/6368167 to your computer and use it in GitHub Desktop.
populating nested subdocuments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mongoose v3.6.18 | |
// | |
var mongoose = require('mongoose'); | |
mongoose.connect('localhost/mydb'); | |
var Schema = mongoose.Schema; | |
var MasterSchema = new Schema({ | |
name: String, | |
}); | |
var PanelSchema = new Schema({ | |
name: String, | |
master: [{type: Schema.Types.ObjectId, ref: 'Master'}] | |
}); | |
var AppSchema = new Schema({ | |
name: String, | |
panel: [{type: Schema.Types.ObjectId, ref: 'Panel'}] | |
}); | |
var App = mongoose.model('App', AppSchema); | |
var Panel = mongoose.model('Panel', PanelSchema); | |
var Master = mongoose.model('Master', MasterSchema); | |
var master = new Master({name:'meester'}); | |
master.save(function(err) { | |
var panel = new Panel({name:'paneel'}); | |
panel.master.push(master); | |
panel.save(function(err) { | |
var app = new App({name:'application'}); | |
app.panel.push(panel); | |
app.save(function(err) { | |
if (err) throw new Error('not found'); | |
App.find({}).populate('panel').populate('master').exec(function(err, prog) { | |
if (err) throw new Error('not found'); | |
console.log('%j',prog); | |
process.exit(); | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect('localhost/mydb'); | |
var Schema = mongoose.Schema; | |
var MasterSchema = new Schema({ | |
name: String, | |
}); | |
var PanelSchema = new Schema({ | |
name: String, | |
master: [{type: Schema.Types.ObjectId, ref: 'Master'}] | |
}); | |
var AppSchema = new Schema({ | |
name: String, | |
panel: [{type: Schema.Types.ObjectId, ref: 'Panel'}] | |
}); | |
var App = mongoose.model('App', AppSchema); | |
var Panel = mongoose.model('Panel', PanelSchema); | |
var Master = mongoose.model('Master', MasterSchema); | |
var master = new Master({name:'meester'}); | |
master.save(function(err) { | |
var panel = new Panel({name:'paneel'}); | |
panel.master.push(master); | |
panel.save(function(err) { | |
var app = new App({name:'application'}); | |
app.panel.push(panel); | |
app.save(function(err) { | |
if (err) throw new Error('not found'); | |
App.find().populate({ path: 'panel' }).exec(function(err, second) { | |
Panel.populate(second,{path:'master'}, function(err, res) { | |
if (err) throw new Error('not found'); | |
console.log('%j', res); | |
process.exit(); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment