mongoose - findOneAndUpdate with $set flag
<p>Consider this command:</p>

<pre><code>    WorkPlan.findOneAndUpdate({ _id: req.params.id }, updateObj, function(err) {
    ...
    })
</code></pre>

<p>versus this:</p>

<pre><code>    WorkPlan.findOneAndUpdate({ _id: req.params.id }, { '$set': updateObj }, function(err) {
    ...
    })
</code></pre>

<p>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 <code>updateObj</code> 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.</p>

<p>Thanks.  </p>

<p>==========</p>

<p>Update:</p>

<p>This is the actual WorkPlan schema definition:</p>

<pre><code>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);
</code></pre>

<p>And this is an example of <code>updateObj</code>:</p>

<pre><code>    var updateObj = {
        projectManagerName: projectManagerName,
        clientEmail: clientEmail,
        clientPhoneNumber: clientPhoneNumber,
        segmentationsToDisplay: segmentationsToDisplay ? segmentationsToDisplay.split(',') : []
    }
</code></pre>

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