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

Scoped variables in MongoCode do not work in map reduce operations (ReferenceError)

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      The following code should not give an error.

      <?php
      $m = new Mongo();
      $db = $m->selectDB('example');
      $c = $db->selectCollection('example');
      $c->save(array('test'=>'foo'));
      $ordered_options['mapreduce'] = 'example';
      $map_str = 'function(){for (var i=0;i<testvar.length;i++){emit(i,
      testvar[i]);}}';
      $map_code = new MongoCode($map_str, array('testvar' => array('a', 'b',
      'c')));
      $reduce_code = new MongoCode('function(key, values)

      {return 1;}

      ');
      $options = array(
      'mapreduce' => 'example',
      'map' => $map_code,
      'reduce' => $reduce_code
      );
      $mr_res = $db->command($options);
      print_r($map_code);
      print_r($mr_res);
      ?>
      OUTPUT
      MongoCode Object
      (
      [code] => function(){for (var i=0;i<testvar.length;i++){emit(i,
      testvar[i]);}}
      [scope] => Array
      (
      [testvar] => Array
      (
      [0] => a
      [1] => b
      [2] => c
      )
      )
      )
      Array
      (
      [errmsg] => assertion: map invoke failed: JS Error:
      ReferenceError: testvar is not defined nofile_b:0
      [ok] => 0
      )

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            mikedransfield Mike Dransfield
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: