我将尝试使用以下代码更新MongoDB文档:
代码执行成功,但是现有的数据库数组值并没有被.save更新。req.body的内容如下(特别注意“start”数组中的值):
.findById成功地从数据库中检索出现有文档,其中只包含“start”数组。
任何关于此处可能发生的情况的指导?我正在使用MongoDB版本3.0.6和Mongoose版本4.1.12,运行在NodeJS版本4.1.1和Express版本4.13.3上。
exports.update = function (req, res) {
if (req.body._id) { delete req.body._id; }
Product.findById(req.params.id, function (err, product) {
if (err) { return handleError(res, err); }
if (!product) { return res.send(404); }
var updated = _.merge(product, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.status(200).json(product);
});
});
};
代码执行成功,但是现有的数据库数组值并没有被.save更新。req.body的内容如下(特别注意“start”数组中的值):
{
"_id" : ObjectId("563a95d9cc2d38622b867ecf"),
"productName" : "Product Name",
"productVersion" : "1",
"productOverview" : "Description of product.",
"productManager" : ObjectId("563a90de195e72712a197d06"),
"businessPriority" : "1 Must Do",
"businessRank" : 2,
"businessFactors" : {
"growth" : true,
"diversification" : true,
"architecture" : false,
"riskMitigation" : false,
"retention" : false
},
"complete" : false,
"phase" : "Discovery",
"comment" : [
"Discovery phase comments",
"Development phase comments",
"Pilot phase comments",
"Pre-launch phase comments",
"Post-launch phase comments"
],
"finish" : [
"2015-11-30",
"2016-03-31",
"2016-05-31",
"2016-06-30",
"2016-08-31"
],
"start" : [
"2015-07-01",
"2015-12-01",
"2016-04-01",
"2016-06-01",
"2016-07-01"
]
}
.findById成功地从数据库中检索出现有文档,其中只包含“start”数组。
{
"_id" : ObjectId("563a95d9cc2d38622b867ecf"),
"start" : [
"07-02",
"12-01",
"04-01",
"06-01",
"07-01"
]
}
lodash .merge函数构造了一个正确的“更新”记录(其数据内容与上面的req.body相同)。
.save执行没有错误,并返回200状态。然而,数据库中文档的内容仍然包含“start”元素的原始数据:
{
"_id" : ObjectId("563a95d9cc2d38622b867ecf"),
"start" : [
"07-02",
"12-01",
"04-01",
"06-01",
"07-01"
],
"businessFactors" : {
"growth" : true,
"diversification" : true
},
"businessPriority" : "1 Must Do",
"businessRank" : 2,
"comment" : [
"Discovery phase comments",
"Development phase comments.",
"Pilot phase comments",
"Pre-launch phase comments",
"Post-launch phase comments"
],
"finish" : [
"2015-11-30",
"2016-03-31",
"2016-05-31",
"2016-06-30",
"2016-08-31"
],
"phase" : "Discovery",
"productManager" : ObjectId("563a90de195e72712a197d06"),
"productName" : "New Product",
"productOverview" : "Description of product.",
"productVersion" : "1",
"__v" : 1
}
以下是Mongoose模式:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var productSchema = new Schema(
{
productName : String,
productVersion : String,
productOverview : String,
productManager : Schema.Types.ObjectId,
businessPriority : String,
businessRank : Number,
businessFactors : {
retention : Boolean,
growth : Boolean,
diversification : Boolean,
architecture : Boolean,
riskMitigation : Boolean
},
start : [ String ],
finish : [ String ],
comment : [ String ],
phase : String,
complete : Boolean
},
{
collection : 'products'
}
);
module.exports = mongoose.model('Product', productSchema);
任何关于此处可能发生的情况的指导?我正在使用MongoDB版本3.0.6和Mongoose版本4.1.12,运行在NodeJS版本4.1.1和Express版本4.13.3上。