我正在尝试使用以下代码更新 MongoDB 文档:
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 成功地从数据库中检索出现有文档,其中仅包含“开始”数组:
{
"_id" : ObjectId("563a95d9cc2d38622b867ecf"),
"start" : [
"07-02",
"12-01",
"04-01",
"06-01",
"07-01"
]
}
lodash .merge 函数构造了一条正确的“更新”记录(其数据内容与上面的 req.body 相同)。
.save 执行无误,并返回 200 状态。但是,数据库中文档的内容仍然包含“开始”元素的原始数据:
{
"_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);
关于这里可能发生的事情的任何指导?我在 NodeJS 4.1.1 版和 Express 4.13.3 版上使用 MongoDb 3.0.6 版和 Mongoose 4.1.12 版。
最佳答案
您可以 findOneAndUpdate 而不是先找到 id 然后保存。如果 id 不存在,它将创建一个新的。如果你不希望它保存一个新的设置 upsert 为 false
Product.findOneAndUpdate({_id:<your id>, {$set: <your merged JSON>}, {upsert:true}, function(err, effected, raw){});
关于node.js - Mongoose .save 不更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537119/