Details

    • Type: New Feature New Feature
    • Status: Open Open
    • Priority: Minor - P4 Minor - P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: planned but not scheduled
    • Component/s: Write Ops
    • Labels:
      None
    • Backport:
      No
    • # Replies:
      8
    • Last comment by Customer:
      true

      Description

      $unshift - like $push, but to the start of the array, and with support for the $each construct like $addToSet.

      $pushToSet - like $push in that the supplied item(s) will end up at the end of the set in the supplied order - maintaining set semantics by removing pre-existing matching elements. Should support the $each construct.

      $unshiftToSet - just like $pushToSet, but adds items at the start of the array.

      The next two are more "nice to have's".

      $push - add support for the $each construct - $pushAll could then be deprecated

      $shift - just like $pop with reversed sign, i.e. {$pop:{a:-1}} is the same as {$shift:{a:1}}

        Issue Links

          Activity

          Hide
          Keith Branton
          added a comment -

          $unshiftToSet would be really useful to me. My application allows users to change the order of lists, usually represented as an array of FKs in mongo. I currently replace the whole document with optimistic locking semantics to achieve this, but I would much rather use modifiers.

          The complication here is concurrent modifications. If user A reads [1,2,3,4,5] and changes it to [1,3,5,2,4] but in the meantime user B has changed the array to [7,1,2,6,4,3], then I would want the array to look like [1,3,5,2,4,7,6] after user A's changes are saved.

          With $unshiftToSet I could use an update with {$unshiftToSet:{a:{$each:[1,3,5,2,4]}}} to achieve this.

          Show
          Keith Branton
          added a comment - $unshiftToSet would be really useful to me. My application allows users to change the order of lists, usually represented as an array of FKs in mongo. I currently replace the whole document with optimistic locking semantics to achieve this, but I would much rather use modifiers. The complication here is concurrent modifications. If user A reads [1,2,3,4,5] and changes it to [1,3,5,2,4] but in the meantime user B has changed the array to [7,1,2,6,4,3] , then I would want the array to look like [1,3,5,2,4,7,6] after user A's changes are saved. With $unshiftToSet I could use an update with {$unshiftToSet:{a:{$each: [1,3,5,2,4] }}} to achieve this.
          Hide
          Keith Branton
          added a comment - - edited

          $pushToSet would be a key component in solving the use-case discussed on this thread: http://groups.google.com/group/mongodb-user/browse_thread/thread/b62bea001b9f81ea#

          The user wants to track the last 10 users to view a given profile - so he needs capped arrays http://jira.mongodb.org/browse/SERVER-991 but also need the most recent visitor to always be at the end of the array in a way that $addToSet doesn't do.

          This interests me because it is also a feature I plan to implement on my web site - very common social networking use-case.

          Show
          Keith Branton
          added a comment - - edited $pushToSet would be a key component in solving the use-case discussed on this thread: http://groups.google.com/group/mongodb-user/browse_thread/thread/b62bea001b9f81ea# The user wants to track the last 10 users to view a given profile - so he needs capped arrays http://jira.mongodb.org/browse/SERVER-991 but also need the most recent visitor to always be at the end of the array in a way that $addToSet doesn't do. This interests me because it is also a feature I plan to implement on my web site - very common social networking use-case.
          Hide
          Keith Branton
          added a comment - - edited

          Scott Hernandez has pointed out that $pushToSet could be performed with a combination of $pull(All) and $push(All) once http://jira.mongodb.org/browse/SERVER-1050 is implemented.

          On further reflection I currently have no use-cases for "pushing" to both ends of any single array, so the $unshift operations could be worked around by storing all the relevant arrays backwards. Presumably that would perform slightly better too since fewer items would need to be moved when a $push happens than with $unshift.

          I still think this proposal is more intuitive, though, and all my user-sorted arrays are pretty small so I doubt I'd notice the performance difference. Seeing the same order in the shell as my application uses is more obvious when debugging, so if it's not to difficult to do I'm still asking

          Show
          Keith Branton
          added a comment - - edited Scott Hernandez has pointed out that $pushToSet could be performed with a combination of $pull(All) and $push(All) once http://jira.mongodb.org/browse/SERVER-1050 is implemented. On further reflection I currently have no use-cases for "pushing" to both ends of any single array, so the $unshift operations could be worked around by storing all the relevant arrays backwards. Presumably that would perform slightly better too since fewer items would need to be moved when a $push happens than with $unshift. I still think this proposal is more intuitive, though, and all my user-sorted arrays are pretty small so I doubt I'd notice the performance difference. Seeing the same order in the shell as my application uses is more obvious when debugging, so if it's not to difficult to do I'm still asking
          Hide
          Eliot Horowitz
          added a comment -

          Not sure if both will happen or one, but marked as related .

          Show
          Eliot Horowitz
          added a comment - Not sure if both will happen or one, but marked as related .
          Hide
          Chris Hanks
          added a comment -

          These would be great - the ability to use $pushToSet on a fixed-size array would really help me with a problem I'm having.

          Show
          Chris Hanks
          added a comment - These would be great - the ability to use $pushToSet on a fixed-size array would really help me with a problem I'm having.
          Hide
          Ben Brockway
          added a comment -

          Being able to insert an item at the start of an array would be very useful for me for some applications I will be developing over the next few years

          Show
          Ben Brockway
          added a comment - Being able to insert an item at the start of an array would be very useful for me for some applications I will be developing over the next few years
          Hide
          TP Diffenbach
          added a comment -

          Here's my use case, and I think it's compelling.

          When I have an array of history, I want to be able to distinguish one item as the current value. Whether that's the zeroth or the last element doesn't much matter.

          Given db.zeroth.insert( { x:1, statusHistory: [

          { status : "approved"}

          ,

          { status : "draft"}

          ,

          {status : "created"}

          ]}),

          I can reference the zeroth element as the "current" value:

          db.zeroth.find(

          { "statusHistory.0.status" : "approved" }

          ); // find all currently in approved status

          So I'm storing the array newest to oldest so that I can reference the newest (current) status at element zero.

          But now I can't easily add a newer status, because it would be pushed to the end of the array.

          If instead I store the array oldest to newest, I can easily push newer statuses, but I can't easily reference the newest.

          The Mongo solution to issues like this with arrays this seems to be "store it (special element, array length, whatever) explicitly as another attribute or sub-document". But that ends up either duplicating data, or fracturing it into a current part and a history part, and it doesn't play well with the New Aggregation Framework (e.g., if I wanted to $unwind all status, including the current).

          But unless I duplicate data, I have no way to be able to query and update the same position: with one array, I can query element zero, or I can push onto the end, but there's no position that I can explicitly both query and update.

          Show
          TP Diffenbach
          added a comment - Here's my use case, and I think it's compelling. When I have an array of history, I want to be able to distinguish one item as the current value. Whether that's the zeroth or the last element doesn't much matter. Given db.zeroth.insert( { x:1, statusHistory: [ { status : "approved"} , { status : "draft"} , {status : "created"} ]}), I can reference the zeroth element as the "current" value: db.zeroth.find( { "statusHistory.0.status" : "approved" } ); // find all currently in approved status So I'm storing the array newest to oldest so that I can reference the newest (current) status at element zero. But now I can't easily add a newer status, because it would be pushed to the end of the array. If instead I store the array oldest to newest, I can easily push newer statuses, but I can't easily reference the newest. The Mongo solution to issues like this with arrays this seems to be "store it (special element, array length, whatever) explicitly as another attribute or sub-document". But that ends up either duplicating data, or fracturing it into a current part and a history part, and it doesn't play well with the New Aggregation Framework (e.g., if I wanted to $unwind all status, including the current). But unless I duplicate data, I have no way to be able to query and update the same position: with one array, I can query element zero, or I can push onto the end, but there's no position that I can explicitly both query and update.
          Hide
          Kevin J. Rice
          added a comment -

          Echo TP Diffenbach's comment. This is EXACTLY what we're doing now, storing data in an array:

          ts=timestamp, val=value

          vals : [ [ts1,val1], [ts2,val2], [ts3,val3], ... ]

          Every minute, I'm adding new data with $push, onto the end of the array.

          But, to keep record sizes at 4k, I have splitRecords.py. It moves off the oldest 125 datapoints to their own document (all during which time a new value may come in while I'm busy pulling this data out). So, I'm pulling from the front of the array. I only get the option of one element at a time. I'd like to pull an arbitrary number of elements from the front of the array.

          My temporary, and ugly, workaround is to use $pullAll and reference the exact elements to be pulled. I'm very lucky in that my data is unique and I can do this. But, it's ugly - I only need to pull n elements off the front of the array, not search for them individually and remove each specially.

          Show
          Kevin J. Rice
          added a comment - Echo TP Diffenbach's comment. This is EXACTLY what we're doing now, storing data in an array: ts=timestamp, val=value vals : [ [ts1,val1] , [ts2,val2] , [ts3,val3] , ... ] Every minute, I'm adding new data with $push, onto the end of the array. But, to keep record sizes at 4k, I have splitRecords.py. It moves off the oldest 125 datapoints to their own document (all during which time a new value may come in while I'm busy pulling this data out). So, I'm pulling from the front of the array. I only get the option of one element at a time. I'd like to pull an arbitrary number of elements from the front of the array. My temporary, and ugly, workaround is to use $pullAll and reference the exact elements to be pulled. I'm very lucky in that my data is unique and I can do this. But, it's ugly - I only need to pull n elements off the front of the array, not search for them individually and remove each specially.

            People

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

              Dates

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