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

Use positional operator to update all items in an array

    Details

      Description

      Given the following:

      > var obj = t.findOne()
      { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
        "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }

      One should be able to modify each item in the comments array by using an update command like the following:

      > t.update( obj, {$set:{'comments.$.votes':1}}, false, true )
      > t.find()
      { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
        "comments" : [ { "by" : "joe", "votes" : 1 }, { "by" : "jane", "votes" : 1 } ] }

        Issue Links

          Activity

          Hide
          jtmarmon Jason Marmon added a comment -

          +1 Use case:

          I have an array of embedded objects representing customers that we automatically contact. After contacting the customer, we update that embedded document and contact the next one some minutes later. If, however, somehow a second copy of that customer's identifier sneaks into the array, our application contacts this customer over and over until we manually stop it, because only the first copy of the customer's status is updates as 'contacted'.

          I agree with the above sentiment - this should already be implemented. It's a bug that needs to be fixed, not an optional new feature.

          Show
          jtmarmon Jason Marmon added a comment - +1 Use case: I have an array of embedded objects representing customers that we automatically contact. After contacting the customer, we update that embedded document and contact the next one some minutes later. If, however, somehow a second copy of that customer's identifier sneaks into the array, our application contacts this customer over and over until we manually stop it, because only the first copy of the customer's status is updates as 'contacted'. I agree with the above sentiment - this should already be implemented. It's a bug that needs to be fixed, not an optional new feature.
          Hide
          sbuljat Stjepan Buljat added a comment -

          +1 this is a must have...

          Show
          sbuljat Stjepan Buljat added a comment - +1 this is a must have...
          Hide
          Björn Björn added a comment -

          +1 I need this to update a document schema (introducing a new default field to all documents)

          Show
          Björn Björn added a comment - +1 I need this to update a document schema (introducing a new default field to all documents)
          Hide
          gregaubert Grégoire Aubert added a comment -

          +1 This feature really needs to be implemented.

          Show
          gregaubert Grégoire Aubert added a comment - +1 This feature really needs to be implemented.
          Hide
          mk_trialfire Max Kremer added a comment -

          +1 This is a major deficiency that needs to be addressed

          Show
          mk_trialfire Max Kremer added a comment - +1 This is a major deficiency that needs to be addressed

            Dates

            • Created:
              Updated:
              Days since reply:
              2 weeks, 5 days ago
              Date of 1st Reply: