title: MongoDB MapReduce Emit Strangeness
body: <p>I'm very much a noob when it comes to MapReduce and I have been pulling my hair out with this issue.  Hopefully someone can give me a hand.  </p>
  
  <p>My Goal: Get the product revenue and a count of the units sold.  </p>
  
  <p>Transactions Collection sample document where i'm querying from: </p>
  
  <pre><code> { "_id" : ObjectId( "xxxxxxxxxx" ),
    "MerchantID" : { "$ref" : "merchants",
      "$id" : ObjectId( "xxxxxxxx" ) },
    "TransactionSocialKey" : "xxxxxxxx",
    "PurchaseComplete: true,
    "Products" : [ 
      { "ProductID" : { "$ref" : "products",
          "$id" : ObjectId( "4ecae2b9cf72ab1f6900xxx1" ) },
        "ProductPrice" : 14.99,
        "ProductQuantity" : "1" }, 
      { "ProductID" : { "$ref" : "products",
          "$id" : ObjectId( "4ecae2b9cf72ab1f690xxx2" ) },
        "ProductPrice" : 14.99,
        "ProductQuantity" : "1" } ],
    "DateTimeCreated" : Date( 1321919161000 ) }
  </code></pre>
  
  <p>As you can see I have an embedded array called Products with the ProductID, Product Price, and Product Quantity.</p>
  
  <p>My Map Function </p>
  
  <pre><code>map = function(){
  
    if(this.PurchaseComplete === true){
  
          this.Products.forEach(function(Product){
  
              if(Product.ProductID.$id.toString() == Product_ID.toString()){
  
                  emit(Product_ID, {
                      "ProductQuantity" : Product.ProductQuantity, 
                      "ProductPrice" : Product.ProductPrice,
                      "ProductID" : Product.ProductID.$id.toString()
                  }); 
  
              }
  
          }); 
      }
  }
  </code></pre>
  
  <p>So with this i'm only going to emit Transactions that were completed. If the Transaction was completed I'm looping through the Products array and if the Product.ProductID.$id is equal to the Product_ID that I set in the MapReduce Scope then I'm going to emit the Product from the set. </p>
  
  <p>For testing sake I've set up my Reduce function as: </p>
  
  <pre><code>reduce = function(key, Product_Transactions){
  
      return {"Transactions" : Product_Transactions}; 
  
  }
  </code></pre>
  
  <p>For some odd reason i'm getting this sort of result: </p>
  
  <pre><code>[results] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [_id] =&gt; MongoId Object
                          (
                              [$id] =&gt; 4ecae2b9cf72ab1f6900xxx1
                          )
  
                      [value] =&gt; Array
                          (
                              [Transactions] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [Transactions] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [ProductQuantity] =&gt; 1
                                                              [ProductPrice] =&gt; 14.99
                                                              [ProductID] =&gt; 4ecae2b9cf72ab1f6900xxx1
                                                          )
  
                                                      [1] =&gt; Array
                                                          (
                                                              [ProductQuantity] =&gt; 1
                                                              [ProductPrice] =&gt; 14.99
                                                              [ProductID] =&gt; 4ecae2b9cf72ab1f6900xxx1
                                                          )
  
                                                         It Continues… 
                                                   )
                                           )
                                     [1] =&gt; Array
                                          (
                                              [ProductQuantity] =&gt; 1
                                              [ProductPrice] =&gt; 12.74
                                              [ProductID] =&gt; 4ecae2b9cf72ab1f6900xxx1
                                          )
  
                                      [2] =&gt; Array
                                          (
                                              [ProductQuantity] =&gt; 1
                                              [ProductPrice] =&gt; 12.74
                                              [ProductID] =&gt; 4ecae2b9cf72ab1f6900xxx1
                                          )
  
                              )
                         )
                     )
               )
  </code></pre>
  
  <p>I'm not sure why I'm getting this odd embedded array.  The emit key is always the same and never changes.  I'm really lost for ideas on where to start trouble shooting.  Any help or guidance would be appreciated.                                      </p>