title: PHP Mongo MapReduce - How to Call Server Loaded JavaScript Functions
body: <p>First, I want to say that this is more of a PHP Mongo Driver issue than MongoDB issue.</p>
  
  <p>I have a problem with calling MapReduce through PHP. I have 3 custom functions inside Mongo for MapReduce: mapItems, reduceItems, finalizeItems.</p>
  
  <p>When I test my functions from inside Mongo Shell, everything works great:</p>
  
  <pre><code>db.loadServerScripts();
  db.runCommand({
    mapreduce: 'items',
    map: mapItems,
    reduce: reduceItems,
    finalize: finalizeItems,
    out: {inline: 1},
    scope: {members: {a1: 0, a2: 0}
  });
  </code></pre>
  
  <p>Now when I try to do the same in PHP Mongo Driver, nothing works.</p>
  
  <pre><code>$db-&gt;command([
    'mapreduce' =&gt; 'items',
    'map' =&gt; 'mapItems',
    'reduce' =&gt; 'reduceItems',
    'finalize' =&gt; 'finalizeItems',
    'out' =&gt; ['inline' =&gt; 1],
    'scope' =&gt; ['members' =&gt; ['a1' =&gt; 0, 'a2' =&gt; 0]]
  ]);
  </code></pre>
  
  <p>I thought maybe I need to use MongoCode for the functions, but that still didn't work:</p>
  
  <pre><code>$db-&gt;command([
    'mapreduce' =&gt; 'items',
    'map' =&gt; new \MongoCode('mapItems'),
    'reduce' =&gt; new \MongoCode('reduceItems'),
    'finalize' =&gt; new \MongoCode('finalizeItems'),
    'out' =&gt; ['inline' =&gt; 1],
    'scope' =&gt; ['members' =&gt; ['a1' =&gt; 0, 'a2' =&gt; 0]]
  ]);
  </code></pre>
  
  <p>Then I thought maybe the PHP driver is being stupid, so I copy pasted the functions into PHP and tried them:</p>
  
  <pre><code>$db-&gt;command([
    'mapreduce' =&gt; 'items',
    'map' =&gt; new \MongoCode($mapFn),
    'reduce' =&gt; new \MongoCode($reduceFn),
    'finalize' =&gt; new \MongoCode($finalizeFn),
    'out' =&gt; ['inline' =&gt; 1],
    'scope' =&gt; ['members' =&gt; ['a1' =&gt; 0, 'a2' =&gt; 0]]
  ]);
  </code></pre>
  
  <p>And this worked. </p>
  
  <p>How do I need to pass the internal functions to Mongo through PHP?</p>