title: Mapreduce split input string into output array
body: <p>I am dealing with documents like the following one:</p>
  
  <pre><code>&gt; db.productData.find({"upc" : "XXX"}).pretty()
  {
      "_id" : ObjectId("538dfa3d44e19b2bcf590a77"),
      "upc" : "XXX",
      "productDescription" : "bla foo bar bla bla fooX barY",
      "productSize" : "",
      "ingredients" : "foo; bar; foo1; bar1.",
      "notes" : "bla bla bla"
  }
  &gt; 
  </code></pre>
  
  <ul>
  <li>I would like to have a document containing, among the fields, a list/array of splitted ingredients (on the <code>;</code>). I want to split the string of the original collection into an array of strings.</li>
  <li>I would like to map only some of the input fields in the output collection.</li>
  <li>I would like to use <code>mapreduce</code> on MongoDB.</li>
  </ul>
  
  <p>I've tried many different ways moving stuff from the <code>map</code> function to the <code>reduce</code> function failing to find a proper solution.
  From all the attempts I performed, now I know I need to check for <code>null</code> values etc, so the following one is my last attempt:</p>
  
  <p>The map function:</p>
  
  <pre><code>var mapperProductData = function () {
  
      var ingredientsSplitted = values.ingredientsString.split(';');
  
      var objToEmit = {barcode : "", description : "", ingredients : []};
  
      // checking for null (is this strictly necessary? why?)
      if ( 
              this.hasOwnProperty('ingredients')
           &amp;&amp; this.hasOwnProperty('productDescription')
           &amp;&amp; this.hasOwnProperty('upc')
         ) {
          for (var i = 0; i &lt; ingredientsSplitted.length; i++) {
              // I want to emit a new document only when I have all the splitted strings inside the array
              if (i == ingredientsSplitted.length - 1) {
                   objToEmit.barcode     = this.upc;
                   objToEmit.description = this.productDescription;
                   objToEmit.ingredients = ingredientsSplitted;
  
                   emit(this.upc, objToEmit);
              }
          }
      }
  };
  </code></pre>
  
  <p>The reduce function:</p>
  
  <pre><code>    var reducerNewMongoCollection = function(key, values) {
  
      return values;
  };
  </code></pre>
  
  <p>The map-reduce call:</p>
  
  <pre><code>db.productData.mapReduce(
      mapperProductData,
      reducerNewMongoCollection,
      {
          out : "newMongoCollection" ,
          query: { "values" : {$exists: true} }
      }
  );
  </code></pre>
  
  <p>I am getting an empty collection in output (<code>newMongoCollection</code> is empty).
  What am I doing wrong?</p>