Mister_L Mister_L - 1 year ago 324
Node.js Question

mongoose - findOneAndUpdate with $set flag

Consider this command:

WorkPlan.findOneAndUpdate({ _id: req.params.id }, updateObj, function(err) {

versus this:

WorkPlan.findOneAndUpdate({ _id: req.params.id }, { '$set': updateObj }, function(err) {

While developing my project, I was surprised to find out that the result of the first command is the same as the result of the second command: the
is merged into the existing record in the database, even in the first case when it is supposed to replace it. Is this a bug in mongoose/mongodb or am I doing something wrong? how can I replace an object on update instead of merging it? I'm using mongoose 4.0.7.




This is the actual WorkPlan schema definition:

workPlanSchema = mongoose.Schema({
planId: { type: String, required: true },
projectName: { type: String, required: true },
projectNumber: { type: String, required: false },
projectManagerName: { type: String, required: true },
clientPhoneNumber: { type: String, required: false },
clientEmail: { type: String, required: true },
projectEndShowDate: { type: Date, required: true },
segmentationsToDisplay: { type: [String], required: false },
areas: [
fatherArea: { type: mongoose.Schema.ObjectId, ref: 'Area' },
childAreas: [{ childId : { type: mongoose.Schema.ObjectId, ref: 'Area' }, status: { type: String, default: 'none' } }]
logoPositions: [
lat: { type: Number, required: true },
lng: { type: Number, required: true }
logoPath: { type: String, required: false },
}, { collection: 'workPlans' });

WorkPlan = mongoose.model('WorkPlan', workPlanSchema);

And this is an example of

var updateObj = {
projectManagerName: projectManagerName,
clientEmail: clientEmail,
clientPhoneNumber: clientPhoneNumber,
segmentationsToDisplay: segmentationsToDisplay ? segmentationsToDisplay.split(',') : []

Therefore, when I'm NOT using the $set flag, I would expect the field
, for example, not to exist in the new record, yet I see it is still there.

Answer Source

Mongoose update treats all top level keys as $set operations (this is made more clear in the older docs: Mongoose 2.7.x update docs).

In order to get the behavior you want, you need to set the overwrite option to true:

WorkPlan.findOneAndUpdate({ _id: req.params.id }, updateObj, { overwrite: true }, function(err) {

See Mongoose Update documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download