繁体   English   中英

MongoDB MapReduce发射怪异

[英]MongoDB MapReduce Emit Strangeness

对于MapReduce来说,我是个菜鸟,而我一直在努力解决这个问题。 希望有人可以帮助我。

我的目标:获取产品收入和已售出产品的数量。

我从以下位置查询的交易收集示例文档:

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

如您所见,我有一个名为Products的嵌入式阵列,其中具有ProductID,Product Price和Product Quantity。

我的地图功能

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数组,并且Product.ProductID。$ id等于我在MapReduce范围中设置的Product_ID,那么我将从集合中发出Product。

为了测试起见,我将Reduce函数设置为:

reduce = function(key, Product_Transactions){

    return {"Transactions" : Product_Transactions}; 

}

由于某种奇怪的原因,我得到了这种结果:

[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
                                        )

                            )
                       )
                   )
             )

我不确定为什么要得到这个奇怪的嵌入式阵列。 发射键始终相同,永不改变。 我真的迷失了从哪里开始排障的想法。 任何帮助或指导,将不胜感激。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM