我如何使Mapreduce按年获得

本文关键字:Mapreduce 何使 | 更新日期: 2023-09-27 17:53:25

任何人都可以帮助我,我的大脑堆叠使这个集合mapreduce,如何获得PurchaseAmount, PurchaseReturnAmount和TotalAmount,与键按"年"。

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

}我想使用这个map reduce到c#

我如何使Mapreduce按年获得

下面是一个使用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 } }