Node.js _当子文档为';应该是的
这是我的两个模式Node.js _当子文档为';应该是的,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,这是我的两个模式 var reviews = new Schema({ scenarioId: { type: Schema.Types.ObjectId, required: true}, authorId: { type: Schema.Types.ObjectId , required:true }, reviewNote: String, subReviews: [subReviewSchema], active: {type: Boolea
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()}
});
以及次级审查的次级方案
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}
});
这是我更新文档的代码
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);
});
});
};
但出于某种原因,每当我向这段代码发送http post时,结果只会添加我在post请求中发送的内容,而不会添加_id_v或任何其他我希望mongoose/mongodb作为样板文件添加的内容。下面是我的数据库中的一个示例文档
{
"__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"
}
]
}
关于为什么没有在subReviews中将_id添加到我的子文档中,有什么想法吗?我猜问题出在父文档中,您会说: 子评论:[子评论模式] 但是您将子模式变量命名为 子评论 不是subReviewSchema。但根据你的帖子,这只是个猜测。我必须一起查看代码才能获得更好的图片,除非是这样 但是这就可以解释了,因为subReviews:只是因为这个命名问题而需要一个对象,而一个对象正是您发布时得到的对象,所以它只是按照预期将其推送到数组中 编辑 我在github上浏览了猫鼬代码,我对上面的答案不太自信,尽管我想这仍然是可能的。然而,在声明模式时,我确实偶然发现了这条评论:
- 嵌套模式时(在上面的示例中为
),始终先声明子模式,然后再将其传递给is parent子模式