_id not being generated for subdocs when It's supposed to

Here are my two schemas

var reviews = new Schema({
    scenarioId: { type: Schema.Types.ObjectId, required: true},
    authorId:  { type: Schema.Types.ObjectId , required:true },
    reviewNote:   String,
    subReviews: [subReviewSchema],
    active: {type: Boolean, default: true},
    display:  {type: Boolean, default: true},
    date : {type: Date, default: Date.now()}
});

and the subscheA for subreviews

var subReviews = new Schema({
    authorId:  { type: Schema.Types.ObjectId, required:true },
    subReviewNote: String,
    date: {type: Date, default: Date.now()},
    active: {type: Boolean, default: true},
    display:  {type: Boolean, default: true}
});

and here is my code that updates the document

exports.addSubReview = function (req, res) {
    var id = req.params.id;
    var update = req.body;//scenario Id

    Review.findById(id, function (err, obj) {
        if (err || !obj) { return res.send(404, { error: err.message }); }

        obj.subReviews.push(update);

        obj.save(function (err) {
            if (err) { return res.send(404, { error: err.message }); }

            return res.send(200, obj);
        });
    });
};

For some reason though whenever I send an http post to this code the results only adds what i send in the post request not _id _v or any other things that I would expect mongoose/mongodb to add as boilerplate. Here is an example document in my database

{
    "__v": 2,
    "_id": "531e3214a30f5f8427830a97",
    "authorId": "52fd0e6df8352c184b000004",
    "reviewNote": "aaaaaaaaaaaaaaaaa",
    "scenarioId": "531a5b80af15cffc051cea67",
    "date": "2014-03-10T21:37:05.230Z",
    "display": true,
    "active": true,
    "subReviews": [
        {
            "subReviewNote": "This is a subReview",
            "authorId": "52fd0e6df8352c184b000004"
        },
        {
            "subReviewNote": "This is a subReview",
            "authorId": "52fd0e6df8352c184b000004"
        }
    ]
} 

Any ideas on why _id is not being added to my subDocs in subReviews?.

Answers


My guess is that the problem is in the parent doc where you say:

subReviews: [subReviewSchema]

But you named the child schema variable

subReviews

Not subReviewSchema. But that's a guess, from what you've posted. I'd have to see the code together to get a better picture, unless this is it.

But this would explain it, since subReviews: is just expecting an Object because of this naming issue - and an object is exactly what it gets when you POST,so it just pushes it into the array as it expects.

EDIT

I poked around in the mongoose code on github and I am less confident in my answer above, although I guess it could still be possible. However, I did stumble upon this comment, when declaring schemas:

  • When nesting schemas, (children in the example above), always declare the child schema first before passing it into is parent.

I have less confidence in my original answer, because it looks like if you named the variable incorrectly, mongoose is going to throw a TypeError


Need Your Help

Is there a way to access Document Setting by code to uncheck the adaptivity box?

vb.net autodesk

I am using vb net and macros to create a file in autodesk inventor 2014, placing and constraining it between two other objects in an assembly file. The part is already adaptive and I also set it to...

iOS Persistent Notification / Quick Launch

iphone objective-c ios ios5

I'm curious if anyone can provide information on how apps like AppSwitcher use the notification center to create persistent quick-launch notifications.