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

$min, $max field update operators

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.5.3
    • Affects Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Fully Compatible

      Add $min/max update modifiers which leave/replace the existing field based on the comparison of a provided value.

      Note: if the field is missing then the value supplied will replace it like it was specified via a $set.

      Examples:

      > db.v.find()
      {_id:1, v:1}
      {_id:2, v:2}
      {_id:3}
      {_id:4}
      > db.v.update({_id:1}, {$min: {v: 2}} // do not change the doc, existing value is smaller
      > db.v.update({_id:1}, {$min: {v: 0}} // set v to 0
      > db.v.findOne({_id:1})
      {_id:1, v:0}
      
      > db.v.update({_id:2}, {$max: {v: 1}} // do not change the doc, existing value is larger
      > db.v.update({_id:2}, {$max: {v: 2}} // do not change the doc, existing value is the same
      > db.v.update({_id:2}, {$max: {v: 200}} // set v to 200
      > db.v.findOne({_id:2})
      {_id:2, v:200}
      
      > db.v.update({_id:3}, {$min: {v: 2}} // set v to 2
      {_id:3, v:2}
      > db.v.update({_id:4}, {$max: {v: 2}} // set v to 2
      {_id:4, v:2}
      

      old description
      I'm working on a realtime website performance analysis tool, were a number of parallel processes import data into a mongodb instance.

      In this context, it would be extremely helpful to have $max and $min update operators which would work similar to $inc.

      { $min :

      { field : value }

      }

      sets field to new value if field does not exist or value is smaller than current field value.

      { $max :

      { field : value }

      }

      sets field to new value if field does not exist or value is larger than current field value.

            Votes:
            38 Vote for this issue
            Watchers:
            33 Start watching this issue

              Created:
              Updated:
              Resolved: