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

Doing Upsert in Subdocuments when performing 'bulk' operations

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      This improvement can be impact to the server performance.

      However, in some situation it can be save our life.

      For example:

      The company offices are stored in a collection.
      'offices.active' field specifies that the office works currently or not. And 'active' status can be changed when needed.
      Also new offices can be added into the collection.

      A fictive document:

      {
          _id: "12",
          offices: [
              { office_id: "1", active: true },
              { office_id: "2", active: false }, 
          ]
      }
      

      What if I need to change the 'active' value of a document and I have to insert a new document if the searched document doesn't exist in the array and I have to do this via a bulk operation?

      var col = db.collection('companies');
      var batch = col.initializeUnorderedBulkOp();
      
      var conditions = {
          _id: "12", "offices.office_id": "3"
      }
      var update = {
          $set: {
              active: true
          }
      }
      
      batch.find(conditions).upsert().updateOne(update);
      

      As everybody can guess it doesn't work.
      However it would be great if it works.

            Assignee:
            mark.agarunov Mark Agarunov
            Reporter:
            efkan efkan turen
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: