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

$min, $max field update operators from another field

    • Type: Icon: Improvement Improvement
    • Resolution: Incomplete
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None

      I've pass the last hours searching for a realiable way to keep a minimun field value up to date.

      For example, you have:

      { minAll: [1,2,3,4], min: 1 }

      For this current case, Its possible to have an index on 'minAll' and query a '$min' operator under aggregate, but (1) the index will grow more so Its more memory expensive and (2) having it precalculated in another field you can get some performance improvements.

      The basic idea is:

          update({"_id":"test"},

      {         "$pull":\{"minAll":1}

      ,
              "$set":{"min":{"$min":"$minAll"}} //should be 2 now
          });

      When you remove or add an element, would be great to be able to update another field based on the new collection this field currently has. Or simply re-index all documents again:

          update({},{
              "$set":{"min":{"$min":"$minAll"}}
          });

      Currently you can do it this way:

         var elem = findAndModify({"_id":"test"},{"$pull":{"minAll":1}});
         var nmin = Math.min(elem.minAll);
         /* Posible other delays */
         findAndModify({"_id":"test"},{"$set":{"min":nmin}});

      I have no problem doing 2 updates, the main problem from my pov is that this is not atomic, so if you have bad luck, under heavy load, in the microseconds you use to calculate 'nmin' the 'minAll' field could have changed and updated 'min' in parallel, so you are basically overwriting the field with old value.

      This is similar to https://jira.mongodb.org/browse/SERVER-1534?focusedCommentId=322573&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-322573 . With current behaviour you can easily keep up to date the max value of an array field modified by $push, but you have no chance to update max if you use a $pull and removed the top value.

      Thanks!

            Assignee:
            asya.kamsky@mongodb.com Asya Kamsky
            Reporter:
            sombra2eternity@gmail.com Marcos Fernándex
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: