I am trying to create the model for my mongodb database using mongoose. This is what I am trying to do:
var Class = mongoose.model(\'Class\', {className: St
Your model definition is incorrect, you should fix like below.
// var Schema = mongoose.Schema;
var User = mongoose.model('User',{
email: String,
classes: [ {type: Schema.Types.ObjectID, ref: 'Class'}]
});
var Class1 = new Class({/*yourDataWillBeHere*/})
Class1.save(function(err, classData) {
var User1 = new User({/*YourDataWillBeHere*/})
User1.classes.push(classData._id);
User1.save(function(err, userData) {
//make something with userData object
})
})
Then you can get fetched data using with populate()
like this
User
.find()
.populate('classes')
.exec()