Core Server
  1. Core Server
  2. SERVER-1243

Use positional operator to update all items in an array

    Details

    • Backport:
      No
    • # Replies:
      48
    • Last comment by Customer:
      true

      Description

      Given the following:
      > var obj = t.find()
      { "_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
          Mitar
          added a comment -

          OK. But even setting a field on all elements of an array is not yet possible?

          And what I am saying is that I do not understand why there is a need for positional operator when you want to update more than one item in an array (and yes, not necessary all).

          Show
          Mitar
          added a comment - OK. But even setting a field on all elements of an array is not yet possible? And what I am saying is that I do not understand why there is a need for positional operator when you want to update more than one item in an array (and yes, not necessary all).
          Hide
          James Hartig
          added a comment -

          Setting a field on all elements is not possible. Throws a "can't append to array using string field name" error.
          You also cannot unset from all elements. Filed that separately because the lastErrorObject looks like it actually did: https://jira.mongodb.org/browse/SERVER-12680

          Show
          James Hartig
          added a comment - Setting a field on all elements is not possible. Throws a "can't append to array using string field name" error. You also cannot unset from all elements. Filed that separately because the lastErrorObject looks like it actually did: https://jira.mongodb.org/browse/SERVER-12680
          Hide
          Tony Mobily
          added a comment -

          How is this even considered "minor"?
          I had no idea this was a problem. I have been scratching my head in front of failing tests that shouldn't fail, and eventually figured out that only one sub-record was being updated.
          This is a major issue. It must be possible to update more than one sub-record in one go. What's even worse, is that there is no clear, easy work-around to this. Running the update many times is horrific and possibly immensely expensive. Getting the full array is a joke.
          Seriously.

          Show
          Tony Mobily
          added a comment - How is this even considered "minor"? I had no idea this was a problem. I have been scratching my head in front of failing tests that shouldn't fail, and eventually figured out that only one sub-record was being updated. This is a major issue. It must be possible to update more than one sub-record in one go. What's even worse, is that there is no clear, easy work-around to this. Running the update many times is horrific and possibly immensely expensive. Getting the full array is a joke. Seriously.
          Hide
          Tony Mobily
          added a comment -

          If any of the MongoDB developers (or anybody else) feel like helping out, I would love to see if there is indeed a way to deal with my use case...
          http://stackoverflow.com/questions/22420712/best-way-to-get-around-bug-1243-in-mongodb-use-positional-operator-to-update-a

          Show
          Tony Mobily
          added a comment - If any of the MongoDB developers (or anybody else) feel like helping out, I would love to see if there is indeed a way to deal with my use case... http://stackoverflow.com/questions/22420712/best-way-to-get-around-bug-1243-in-mongodb-use-positional-operator-to-update-a
          Hide
          Roman
          added a comment -

          This is very expected feature! Please implement it asap!

          Show
          Roman
          added a comment - This is very expected feature! Please implement it asap!

            Dates

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