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

Add $unshift modifier as alias to $push $each $position: 0

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Query Optimization

      Re-filing this as an individual request from SERVER-2362, focusing only on $unshift.

      I would like to see $unshift implemented simply as an alias to $push with $position: 0, but without requiring $each, because it doesn't read super intuitively:

      Before:

      $push: {
          scores: {
              $each: [ 50, 60, 70 ],
              $position: 0
          }
      }
      

      After:

      $unshift: {
          scores: [ 50, 60, 70 ]
      }
      

      Much cleaner.

      Use cases

      There are at least four questions on StackOverflow asking how to insert elements at the beginning of an array:

      1. http://stackoverflow.com/questions/7936019/how-do-i-add-a-value-to-the-top-of-an-array-in-mongodb
      2. http://stackoverflow.com/questions/13649010/add-values-to-the-beginning-of-an-array-with-mongodb
      3. http://stackoverflow.com/questions/10094573/use-mongodb-array-as-stack
      4. http://stackoverflow.com/questions/10131957/can-you-have-mongo-push-prepend-instead-of-append

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            dandv Dan Dascalescu
            Votes:
            5 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: