I am trying to update a MongoDB document with the following code:
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);
});
});
};
The code executes successfully, but the existing database array values are not updated by the .save. The content of req.body is as follows (of particular note are the values in the "start" array):
{
"_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"
]
}
The .findById successfully retrieves the existing document out of the database, which contains only the "start" array:
{
"_id" : ObjectId("563a95d9cc2d38622b867ecf"),
"start" : [
"07-02",
"12-01",
"04-01",
"06-01",
"07-01"
]
}
The lodash .merge function construct a correct "updated" record (which has the same data content as req.body above).
The .save executes without error, and a 200 status is returned. However, the content of the document in the database still contains the original data for the "start" element:
{
"_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
}
The Mongoose Schema is as follows:
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);
Any guidance on what might be happening here? I am using MongoDb version 3.0.6 and Mongoose version 4.1.12 on NodeJS version 4.1.1 and Express version 4.13.3.