title: MongoDB Map/Reduce raise exception: failed: db assertion failure
body: <p>I created map/reduce functions to group tasks results in one result object. I wrote in in python with using pymongo library:</p>
  
  <pre><code>    m = Code("""function() {
          data = {};
          res = ''
          if(this.result_id) {
              res={'objectid':this.result_id.toString()};
          } else {
              res=this.result;
          }
          emit(this.data, res);
      }""")
      r = Code("""function(k,values) { 
          data={};
          for(var i=0; i&lt;values.length; i++ ) {
              for(attr in values[i])
                  data[attr]=values[i][attr];
          }
          return data
      }""")
  </code></pre>
  
  <p>And i need that result object be in the same order as input tasks queries. But when i use sort param in request:</p>
  
  <pre><code>   res = db.tasks.map_reduce(m, r, query={'job_id':job_id},sort={'position':pymongo.ASCENDING})
  </code></pre>
  
  <p>But this raise exception in mongodb:</p>
  
  <pre><code>    Traceback (most recent call last):
        File "/usr/local/lib/python2.6/dist-packages/gevent/greenlet.py", line 403, in run
          result = self._run(*self.args, **self.kwargs)
        File "/data/www/public/app/seotools/daemon/scripts/mainconverter.py", line 129, in work
          res = autoreconnect(self.db.tasks.map_reduce,m, r, query={'job_id':job_id},sort={'position':1})
        File "/data/www/public/app/seotools/daemon/lib/db/mongo.py", line 95, in autoreconnect
          result = func(*args,**kwargs)
        File "/usr/local/lib/python2.6/dist-packages/pymongo-1.8.1-py2.6-linux-x86_64.egg/pymongo/collection.py", line 945, in map_reduce
          map=map, reduce=reduce, **kwargs)
        File "/usr/local/lib/python2.6/dist-packages/pymongo-1.8.1-py2.6-linux-x86_64.egg/pymongo/database.py", line 294, in command
          (command, result["errmsg"]))
      OperationFailure: command SON([('mapreduce', u'tasks'), ('sort', {'position': 1}), ('query', {'job_id': ObjectId('4d0b30909c7684b60e000000')}), ('reduce', Code('function(k,values) { 
          data={};
          for(var i=0; i&lt;values.length; i++ ) {
              for(attr in values[i])
                  data[attr]=values[i][attr];
         }
          return data
      }', {})), ('map', Code("function() {
          data = {};
          res = ''
          if(this.result_id) {
              res={'objectid':this.result_id.toString()};
          } else {
              res=this.result;
          }
          emit(this.data, res);
      }", {}))]) failed: db assertion failure
  </code></pre>
  
  <p>When i use the same query without sort param:</p>
  
  <pre><code>   res = db.tasks.map_reduce(m, r, query={'job_id':job_id})
  </code></pre>
  
  <p>It's work pretty fine.</p>
  
  <p>Where can be the problem?</p>