[SERVER-26371] Changes made through `updateOne` in Bulk operations report as nUpserted Created: 28/Sep/16  Updated: 19/Nov/16  Resolved: 19/Nov/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.2.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Xavier Del Castillo Assignee: Kelsey Schubert
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

I'm currently running a bulk operation to rename a property of a subdocument stored in an array. Since it's not possible to do a `$rename` for my use case, I'm doing a bulk operation, however the BulkWriteResult returned seems to be misleading and incorrect - in contrast, the actual update is done properly, however I can't be sure that it was done correctly by just looking at the BulkWriteResult alone.

This is a sample document:

{ _id: '3',
  state: 'active',
  stages:
   [ { id: '1',
       scheduledAt: '2016-08-30T20:23:04.355Z',
       state: 'processing',
       updatedAt: Wed Sep 28 2016 09:34:25 GMT-0700 (MST) } ],
  updatedAt: Wed Sep 28 2016 09:34:25 GMT-0700 (MST) 
}

And this is my bulk query that attempts to rename the `scheduledAt` property for the subdocument:

    bulk.find({
      _id: recipientId,
      state: 'active',
      stages: {
        $elemMatch: {
          id: stageId,
          state: 'processing'
        }
      }
    }).updateOne({
      $set: {
        updatedAt: new Date(),
        'stages.$.processedAt': new Date(),
        'stages.$.state': 'processed',
        'stages.$.updatedAt': new Date(),
        'stages.$.wasScheduledAt': 'stages.$.scheduledAt'
      }
    });
 
    bulk.find({
      _id: recipientId,
      state: 'active',
      stages: {
        $elemMatch: {
          id: stageId
        }
      }
    }).updateOne({
      $unset: {
        'stages.$.scheduledAt': ''
      }
    });

And this is my BulkWriteResult:

{ writeErrors: [],
  writeConcernErrors: [],
  nInserted: 0,
  nUpserted: 2,
  nMatched: 0,
  nModified: 0,
  nRemoved: 0,
  upserted: [],
  ok: 1 
}

Which is very misleading, because according to the documentation, nUpserted is set when explicitly using `upsert` which I'm not using, also `nMatched` value is 0, so from this result alone, it seems to me like 2 new documents were upserted as result of the bulk operation.

By Inspecting the document that I wanted to update, I can tell that the update was done correctly, also I'm sure it's not upserting documents because the collection count before and after the bulk op doesn't change.



 Comments   
Comment by Kelsey Schubert [ 19/Nov/16 ]

Hi xdc,

Since we are unable to reproduce this issue and haven't heard back. I am closing this ticket. If this is still an issue for you please provide additional information to help use investigate and we will reopen the ticket.

Regards,
Thomas

Comment by Kelsey Schubert [ 28/Sep/16 ]

Hi xdc,

Thanks for opening this ticket. I'm not seeing the same behavior that you describe on MongoDB 3.2.6, would you please look over the reproduction script below and the resulting output:

db.foo.drop()
 
db.foo.insert({ _id: '3',
    state: 'active',   
    stages: [ { id: '1',scheduledAt: '2016-08-30T20:23:04.355Z', state: 'processing'}]
})
 
recipientId = '3'
stageId = '1'
 
bulk = db.foo.initializeOrderedBulkOp()
bulk.find({
  _id: recipientId,
  state: 'active',
  stages: {
    $elemMatch: {
      id: stageId,
      state: 'processing'
    }
  }
}).updateOne({
  $set: {
    updatedAt: new Date(),
    'stages.$.processedAt': new Date(),
    'stages.$.state': 'processed',
    'stages.$.updatedAt': new Date(),
    'stages.$.wasScheduledAt': 'stages.$.scheduledAt'
  }
});
 
bulk.find({
  _id: recipientId,
  state: 'active',
  stages: {
    $elemMatch: {
      id: stageId
    }
  }
}).updateOne({
  $unset: {
    'stages.$.scheduledAt': ''
  }
});
 
print(bulk.execute())

After running this I see the following output:

BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nMatched" : 2,
	"nModified" : 2,
	"nRemoved" : 0,
	"upserted" : [ ]
})

Thank you,
Thomas

Generated at Thu Feb 08 04:11:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.