0

任何人都可以帮助我,我的大脑堆积使这个集合到 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
    }

我想用这个映射减少到 C#

4

1 回答 1

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 } }   
于 2013-01-16T13:53:46.223 回答