Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an embedded document within an embedded document in mongoose?

I am building an API in node.js which uses mongodb and mongoose. Currently I have an embedded document within an embedded document (Schema within a Schema) that is simply not persisted to the database, and I have tried all I can but no luck.

I have the Schema's defined in mongoose as:

var BlogPostSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  author: { type: ObjectId, ref: "User" },
  title: { type: String },
  body: { type: String },
  comments: [CommentSchema]
});

var CommentSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  user: { type: ObjectId, ref: "User" },
  body: { type: String, default: "" },
  subComments: [SubCommentSchema]
});

var SubCommentSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  user: { type: ObjectId, ref: "User" },
  body: { type: String, default: "" }
});

And the code I execute is as follows:

// Create a comment
app.post("/posts/:id/comments", function(req, res, next) {
  Posts.find({ _id : req.params.id }, function(err, item){
    if(err) return next("Error finding blog post.");                
    item[0].comments.push(new Comment(JSON.parse(req.body)));
    item[0].save(); // <= This actually saves and works fine
    respond(req, res, item[0].comments, next);
  });
});

// Create a subcomment
app.post("/posts/:id/comments/:commentid/subcomments", function(req, res, next) {
  Posts.find({ _id : req.params.id }, function(err, item){
    if(err) return next("Error finding blog post.");
    item[0].comments[req.params.commentid - 1].subcomments.push(new SubComment(JSON.parse(req.body)));
    item[0].save(); // <= This completes (without error btw) but does not persist to the database
    respond(req, res, item[0].comments[req.params.commentid - 1].subcomments, next);
  });
});

I can create Blog Posts with comments without problem, but for some reason I can not create subcomments on a comment. The Blog Post document actually has the comments and subcomments attached when printing to the console during execution - only it does not save to the database (it saves the Blog Post with a comment, but no subcomments).

I have tried to "markModified" on the comments array, but no change:

Posts.markModified("comments"); // <= no error, but also no change
...
Posts.comments.markModified("subcomments"); // <= produces an error: "TypeError: Object [object Object] has no method 'markModified'"
like image 825
Rory Avatar asked May 01 '12 19:05

Rory


2 Answers

Problem since solved. I was given the answer by Aaron Heckmann over on the mongoose Google Group:

Always declare your child schemas before passing them to you parent schemas otherwise you are passing undefined.

SubCommentSchema should be first, then Comment followed by BlogPost.

After reversing the schemas it worked.

like image 116
Rory Avatar answered Sep 28 '22 12:09

Rory


I thing the updating of a document is not an important issue as the embedded documents are also fully capable of any services.

like image 36
postcard printing Avatar answered Sep 28 '22 10:09

postcard printing