Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major - P3 Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Backport:
      No
    • # Replies:
      3
    • Last comment by Customer:
      true
    • Driver changes needed?:
      Driver changes needed

      Description

      Currently $push() only pushes items onto the end of the array.
      It would be nice if we could pass an optional boolean parameter that would allow pushing to the front of the array instead.

      This would allow reversing the order of arrays by nature of the front-push update, and would eliminate the need to do any client side sorting or reversing.
      Currently adding array items atomically in reverse chronological order is not possible.

      This is similar to SERVER-2036, and having an $insert() would accomplish the same thing.
      But more specific and easier.

        Issue Links

          Activity

          Hide
          D
          added a comment -

          What if I have an embedded array like this:
          {
          "_id" : "1"
          "values" : [
          "A" :

          { "A": "A", "num" : 1 }

          ,
          "B" :

          { "B": "B", "num" : 2 }

          ,
          "C" :

          { "C": "C", "num" : 3 }

          ]
          }

          And I update one array element with the positional operator:
          http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

          t.update(

          { '_id':'1' }

          , { $inc:{ 'values.$.num':2 }}, false, true ).

          But now someone has reordered in between fetch and update, the update will match the wrong element in the array.

          So how are we supposed to update array elements safely when there is no support for natural Id's in mongo?

          Show
          D
          added a comment - What if I have an embedded array like this: { "_id" : "1" "values" : [ "A" : { "A": "A", "num" : 1 } , "B" : { "B": "B", "num" : 2 } , "C" : { "C": "C", "num" : 3 } ] } And I update one array element with the positional operator: http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator t.update( { '_id':'1' } , { $inc:{ 'values.$.num':2 }}, false, true ). But now someone has reordered in between fetch and update, the update will match the wrong element in the array. So how are we supposed to update array elements safely when there is no support for natural Id's in mongo?
          Hide
          Scott Hernandez
          added a comment -

          Your document is invalid and so is your update using "$". I think you want this:

           t.update( { '_id':'1', "values.A":"A" }, { $inc:{ 'values.$.num':2 }}, false, true ) 
          where you have to use a query to find the array element you want to update using the positional operator.

          Please post on http://groups.google.com/group/mongodb-user/ if you have usage questions.

          Show
          Scott Hernandez
          added a comment - Your document is invalid and so is your update using "$". I think you want this: t.update( { '_id':'1', "values.A":"A" }, { $inc:{ 'values.$.num':2 }}, false, true ) where you have to use a query to find the array element you want to update using the positional operator. Please post on http://groups.google.com/group/mongodb-user/ if you have usage questions.
          Hide
          D
          added a comment -

          This was not a question of usage (of the positional operator), I know how to do this.

          But a remark on how the positional operator becomes unsafe by the proposed new feature.

          Show
          D
          added a comment - This was not a question of usage (of the positional operator), I know how to do this. But a remark on how the positional operator becomes unsafe by the proposed new feature.

            People

            • Votes:
              8 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                2 years, 1 week, 6 days ago
                Date of 1st Reply: