Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-26371

Changes made through `updateOne` in Bulk operations report as nUpserted

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: Major - P3 Major - P3
    • None
    • 3.2.6
    • Querying
    • None
    • ALL

    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.

      Attachments

        Activity

          People

            kelsey.schubert@mongodb.com Kelsey Schubert
            xdc Xavier Del Castillo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: