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

MapReduceResult.find() and drop() error when outputting to a different DB

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.1.8
    • Component/s: MapReduce, Shell
    • None
    • ALL
    • Hide
      t = db.mr;
      t.drop();
      
      t.save( { x : 1 , tags : [ "a" , "b" ] } );
      t.save( { x : 2 , tags : [ "b" , "c" ] } );
      t.save( { x : 3 , tags : [ "c" , "a" ] } );
      t.save( { x : 4 , tags : [ "b" , "c" ] } );
      
      m = function(){
          this.tags.forEach(
              function(z){
                  emit( z , { count : 1 } );
              }
          );
      };
      
      r = function( key , values ){
          var total = 0;
          for ( var i=0; i<values.length; i++ ){
              total += values[i].count;
          }
          return { count : total };
      };
      
      res = t.mapReduce(m , r , {out : { replace: "outColl", db: "outDb" } });
      outDB.outColl.find().count()
      res.find().count() // error, no results
      
      Show
      t = db.mr; t.drop(); t.save( { x : 1 , tags : [ "a" , "b" ] } ); t.save( { x : 2 , tags : [ "b" , "c" ] } ); t.save( { x : 3 , tags : [ "c" , "a" ] } ); t.save( { x : 4 , tags : [ "b" , "c" ] } ); m = function(){ this .tags.forEach( function(z){ emit( z , { count : 1 } ); } ); }; r = function( key , values ){ var total = 0; for ( var i=0; i<values.length; i++ ){ total += values[i].count; } return { count : total }; }; res = t.mapReduce(m , r , {out : { replace: "outColl" , db: "outDb" } }); outDB.outColl.find().count() res.find().count() // error, no results

      DBCollection's mapreduce function returns a MapReduceResult object, which takes in the result of db.runCommand(mapreduce).

      If the output is to a different db (i.e. when the "out" parameter is specified with an action instead of a collection name), the result is an object instead of a collection name string.

      This is passed into MapReduceResult and stored as the collection name, which in this case gets parsed as the string "[object BSON]". This variable is used by MapReduceResult.find() and MapReduceResult.drop().

      The two functions are not documented so maybe they could be cleaned up a bit.

            Assignee:
            robert.guo@mongodb.com Robert Guo (Inactive)
            Reporter:
            robert.guo@mongodb.com Robert Guo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: