I have a schema like:
var SchComments = new Schema({
title : String
, body : String
, date : Date
});
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, date : Date
, comments : [SchComments ]
, meta : {
votes : Number
, favs : Number
}
});
How can I insert value to nested schema. How can I call SchComments to pass value in Node.JS
To add an element to a post's comments array, you can either call push on the array and then save the change:
var post = new BlogPostModel({});
post.comments.push({title: 'a', body: 'b', date: new Date()});
post.save(callback);
Or use a $push operator in an update:
BlogPostModel.update({}, {$push: {comments: {
title: 'a2', body: 'b2', date: new Date()
}}}, callback);
There are other ways, but those are the most typical.