Home > Net >  How to update an embedded document within an embedded document in mongoose?
How to update an embedded document within an embedded document in mongoose?

Time:09-10

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'"

CodePudding user response:

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.

CodePudding user response:

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

  • Related