2012-02-17 10 views
0

MapReduceについて私は非常にうなずきますが、私はこの問題を解決しています。うまくいけば誰かが私に手を差し伸べることができる。MongoDB MapReduce Emit Strangeness

マイゴール:製品の収益と販売台数を取得します。あなたは、私が埋め込まれた商品コードと製品という配列、製品価格、および製品の数量を持って見ることができるように

{ "_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) } 

:私はから照会てる

取引コレクションサンプルドキュメント。

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() 
       }); 

      } 

     }); 
    } 
} 

ので、私は唯一の完了した取引を放出するつもりですこれでマイマップ機能。トランザクションが完了した場合、私はProducts配列をループしています.ProductID。$ idがMapReduceスコープで設定したProduct_IDと等しい場合は、セットからプロダクトを発行します。私がなぜわからない

[results] => Array 
     (
      [0] => Array 
       (
        [_id] => MongoId Object 
         (
          [$id] => 4ecae2b9cf72ab1f6900xxx1 
         ) 

        [value] => Array 
         (
          [Transactions] => Array 
           (
            [0] => Array 
             (
              [Transactions] => Array 
               (
                [0] => Array 
                 (
                  [ProductQuantity] => 1 
                  [ProductPrice] => 14.99 
                  [ProductID] => 4ecae2b9cf72ab1f6900xxx1 
                 ) 

                [1] => Array 
                 (
                  [ProductQuantity] => 1 
                  [ProductPrice] => 14.99 
                  [ProductID] => 4ecae2b9cf72ab1f6900xxx1 
                 ) 

                 It Continues… 
               ) 
             ) 
            [1] => Array 
             (
              [ProductQuantity] => 1 
              [ProductPrice] => 12.74 
              [ProductID] => 4ecae2b9cf72ab1f6900xxx1 
             ) 

            [2] => Array 
             (
              [ProductQuantity] => 1 
              [ProductPrice] => 12.74 
              [ProductID] => 4ecae2b9cf72ab1f6900xxx1 
             ) 

          ) 
         ) 
        ) 
      ) 

:私は、結果のこの種を取得していますいくつかの奇妙な理由で

reduce = function(key, Product_Transactions){ 

    return {"Transactions" : Product_Transactions}; 

} 

:私は私としての機能を削減を設定してテスト便宜上

私はこの奇妙な埋め込み配列を得ている。発光キーは常に同じで、決して変更されません。私はトラブルシューティングをどこから始めるべきかというアイデアのために本当に失われています。どんな助けや指導も頂ければ幸いです。

答えて

1

mapの出力は、reduceが消費して生成するフォーマットと同じである必要があります。その考え方は、reduceが並行して実行される可能性があります。ここで

は、あなたのコードは、(擬似コード)のようになります方法です

var map = function() { 
    if(some condition) { 
    emit(product_id, {Transactions: [{ // <= note the array here! 
         "ProductQuantity" : Product.ProductQuantity, 
         "ProductPrice" : Product.ProductPrice, 
         "ProductID" : ID 
        }]}) 
    } 
}; 

var reduce = function(key, vals) { 
    var result = {Transactions: []}; 

    vals.forEach(function(v) { 
    v.Transactions.forEach(t) { 
     result.Transactions.push(t); 
    } 
    }); 

    return result; 
} 
+0

は、迅速な返事をどうもありがとうございます。私はあなたの提案を取り乱して、これがどこにでも入るかどうかを見ていきます。 – whobutsb

+0

これについての私の大きな質問の1つは、放射が多次元配列を生成するのはなぜですか?私は実際には一次元の連想配列を探しています。なぜ2つのトランザクションキーがあるのか​​わかりません。 – whobutsb

+0

Emitは正常に動作しています。物事を混乱させるのはあなたの削減です。私のアドバイスを試しましたか? –