I'm thinking that the best way to do this is:
/ ** * Article schema * / var PostSchema = new schema ({title: {type: string, required: true, trim: true}, author: {type: string, required: true, default: 'whisher'}, slug: {type : String, index: {unique: true}}, body: {type: string, essential: truth, trim: true}, avatar: {type: string, essential: true}, position: {type: string, essential: true , Type: [string]}, tag: {type} {}: type: correct}, built: { Type: [string], required: true, index: true}, comment: {type: date, required: correct, default: date.nuh}, published: {type: date, required: true} Schema.OGJ CtId, Ref: 'CommentSchema'}, Meta: {vote: {type: number, default: 0}, comments: {type: number, default: 0}}}); / ** * Comment schema * / var CommentSchema = new schema ({post_id: {type: Schema.Types.ObjectId, ref: 'post', required: true}, author: {type: string, required: true}, email : {Type: string, essential: true}, web: {type: string}, body: {type: string, essential: true, trim: true}, position: {type: string, essential: truth, default: 'pending '}, Created: {type: date, required: true, default: date.nuh}, meta: {vote: number}}); / ** * Create a comment * / export.create = function (req, res) {var Comment = new comment (req.body); Comment.save (function {err} {return res.jsonp (500, {error: 'can not save comment}')}} Post.findById (comment.post_id) .exec (function , Post)) (if) {return res.jsonp (404, {error: 'Failed to load posts with ID'; comment.post_id});} if (! Post) {return res.jsonp (404) , Failed to load post with {error: id '+ comment.post_id};} post.meta.comments = post.meta.comments ++; post.save (function) {if (err) {return res .jsonp (500, {error: 'Post can not be updated'})}} res.jsonp (200, comment);})}}}}}}};
< P> I'm just looking at BTW but this way:
Model.findByIdAndUpate ( Comment.post_id, {post.meta. Comments: post.meta.comments ++})
does not work
I think you have to use the operator to increase the number of such comments ...
post.findbid and update (Comment. Post_id, {$ inc: {"meta.comments": 1}}, call back);
Comments
Post a Comment