我如何使Mapreduce逐年获得成功?

时间:2013-01-16 08:50:39

标签: c# mongodb

任何人都可以帮助我, 我的大脑堆叠使这个集合mapreduce, 我如何获得PurchaseAmount,PurchaseReturnAmount和TotalAmount,键“年”。<​​/ strong>

{
    "_id" : {
            "OwnerId" : "coba@aja.com",
            "SupplierId" : BinData(3,"AYC8In8bFkGYNx34poQLlg=="),
            "GroupId" : BinData(3,"mzWBCilngEGd72YpeyijcQ=="),
            "ProductId" : BinData(3,"gZ2g/syue06v8b88+0pqRA=="),
            "Date" : ISODate("2013-01-15T00:00:00Z")
    },
    "value" : {
            "OwnerId" : "oetawan@dokuku.com",
            "Date" : ISODate("2013-01-15T00:00:00Z"),
            "SupplierId" : BinData(3,"AYC8In8bFkGYNx34poQLlg=="),
            "SupplierName" : "Matahari",
            "GroupId" : BinData(3,"mzWBCilngEGd72YpeyijcQ=="),
            "GroupCode" : "Umum",
            "GroupName" : "Umum",
            "ProductId" : BinData(3,"gZ2g/syue06v8b88+0pqRA=="),
            "ProductCode" : null,
            "ProductBarcode" : "IPAD2",
            "ProductName" : "iPad 2",
            "PurchaseAmount" : 19500000,
            "PurchaseReturnAmount" : 0,
            "TotalAmount" : 19500000
    }

} 我想使用这个地图reduce to C#

1 个答案:

答案 0 :(得分:0)

这是使用MR的shell示例。当然,您也可以使用聚合框架来完成它,但可能您希望在后台运行它;

db.so.mapReduce(
        function () { /* map */
            var key = this._id.Date.getFullYear();
            emit(key, { PurchaseAmount:this.value.PurchaseAmount, PurchaseReturnAmount:this.value.PurchaseReturnAmount, TotalAmount:this.value.TotalAmount } );
        }, 
        function (key, array) { /* reduce*/
            var result = { PurchaseAmount: 0, PurchaseReturnAmount: 0, TotalAmount: 0 };
            for (var i = 0; i < array.length; i++) {
                {
                    result.PurchaseAmount += array[i].PurchaseAmount;
                    result.PurchaseReturnAmount += array[i].PurchaseReturnAmount;
                    result.TotalAmount += array[i].TotalAmount;
                }
            }
            return result;
        },
        {
            out: "so_mr_example",
            query:{}
        }
);

db.so_mr_example.find();

产生类似的东西;

{ "_id" : 2013, "value" : { "PurchaseAmount" : 39000000, "PurchaseReturnAmount" : 0, "TotalAmount" : 39000000 } }
{ "_id" : 2014, "value" : { "PurchaseAmount" : 19500000, "PurchaseReturnAmount" : 0, "TotalAmount" : 19500000 } }