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: String, marks: [{type: Number}], grades: [{type: Number}]});
var User = mongoose.model('User', {email: String, classes: [Class] });
//Lets create a new user
var class1 = new Class({className: 'aaa', marks: [72, 88, 63], grades: [30, 40, 30]});
var user1 = new User({email: 'aaa@some.com', classes: [class1]});
Saving class1
seems to work okay but when I check mongodb, this is displayed:
{
"_id" : ObjectId("someId"),
"className" : "TEST1234",
"grades" : [ 30, 40, 30 ],
"marks" : [ 72, 88, 63 ],
"__v" : 0
}
What is "__v : 0"
?
Saving the user is not successful at all, this is the following error:
ValidationError: CastError: Cast to Array failed for value "{ marks: [ 72, 88, 63 ], grades: [ 30, 40, 30 ], _id: someId, className: 'TEST1234' }" at path "classes" `
What exactly does the error mean? Why is it casting anything to a array? Shouldn't classes: [Class]
be an array of type class
?
Just for Update
Now Mongoose supports subdocuments, which are the documented way to nest arrays,
Sources
http://mongoosejs.com/docs/schematypes.html