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

Doing Upsert in Subdocuments when performing 'bulk' operations

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Major - P3
    • Resolution: Duplicate
    • None
    • None
    • None

    Description

      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.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: