且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用汇总计算运行总额

更新时间:2023-01-29 23:21:30

实际上,至少比聚合框架更适合 mapReduce 在最初的问题解决中.聚合框架不具有先前文档的值或文档的先前分组"值的概念,因此这就是为什么它不能做到这一点.

Actually more suited to mapReduce than the aggregation framework, at least in the initial problem solving. The aggregation framework has no concept of the value of a previous document, or the previous "grouped" value of a document so this is why it cannot do this.

另一方面,mapReduce具有一个全局范围",可以在处理阶段和文档之间共享它们.这将为您提供所需日末当前余额的运行总额".

On the other hand, mapReduce has a "global scope" that can be shared between stages and documents as they are processed. This will get you the "running total" for the current balance at end of day you require.

db.collection.mapReduce(
  function () {
    var date = new Date(this.dateEntry.valueOf() -
      ( this.dateEntry.valueOf() % ( 1000 * 60 * 60 * 24 ) )
    );

    emit( date, this.amount );
  },
  function(key,values) {
      return Array.sum( values );
  },
  { 
      "scope": { "total": 0 },
      "finalize": function(key,value) {
          total += value;
          return total;
      },
      "out": { "inline": 1 }
  }
)      

这将按日期分组求和,然后在完成"部分中进行每天的累计.

That will sum by date grouping and then in the "finalize" section it makes a cumulative sum from each day.

   "results" : [
            {
                    "_id" : ISODate("2015-01-06T00:00:00Z"),
                    "value" : 50
            },
            {
                    "_id" : ISODate("2015-01-07T00:00:00Z"),
                    "value" : 150
            },
            {
                    "_id" : ISODate("2015-01-09T00:00:00Z"),
                    "value" : 179
            }
    ],

从长远来看,***是每天单独收集一个条目,并使用 $inc 在每个开头的 upsert 创建一个新文档以结转前一天的余额:

In the longer term you would be best of having a separate collection with an entry for each day an alter the balance using $inc in an update. Just also do an $inc upsert at the beginning of each day to create a new document carrying forward the balance from the previous day:

// increase balance
db.daily(
    { "dateEntry": currentDate },
    { "$inc": { "balance": amount } },
    { "upsert": true }
);

// decrease balance
db.daily(
    { "dateEntry": currentDate },
    { "$inc": { "balance": -amount } },
    { "upsert": true }
);

// Each day
var lastDay = db.daily.findOne({ "dateEntry": lastDate });
db.daily(
    { "dateEntry": currentDate },
    { "$inc": { "balance": lastDay.balance } },
    { "upsert": true }
);


如何不这样做

尽管的确如此,但是由于最初的写作有更多的运算符引入了聚合框架,所以这里所要求的仍然不是在聚合语句中实际可行的.


How NOT to do this

Whilst it is true that since the original writing there are more operators introduced to the aggregation framework, what is being asked here is still not practical to do in an aggregation statement.

同样的基本规则适用,聚合框架 不能引用先前的文档"中的值,也不能存储全局变量".通过将所有结果强制转换为数组来黑客" :

The same basic rule applies that the aggregation framework cannot reference a value from a previous "document", nor can it store a "global variable". "Hacking" this by coercion of all results into an array:

db.collection.aggregate([
  { "$group": {
    "_id": { 
      "y": { "$year": "$dateEntry" }, 
      "m": { "$month": "$dateEntry" }, 
      "d": { "$dayOfMonth": "$dateEntry" } 
    }, 
    "amount": { "$sum": "$amount" }
  }},
  { "$sort": { "_id": 1 } },
  { "$group": {
    "_id": null,
    "docs": { "$push": "$$ROOT" }
  }},
  { "$addFields": {
    "docs": {
      "$map": {
        "input": { "$range": [ 0, { "$size": "$docs" } ] },
        "in": {
          "$mergeObjects": [
            { "$arrayElemAt": [ "$docs", "$$this" ] },
            { "amount": { 
              "$sum": { 
                "$slice": [ "$docs.amount", 0, { "$add": [ "$$this", 1 ] } ]
              }
            }}
          ]
        }
      }
    }
  }},
  { "$unwind": "$docs" },
  { "$replaceRoot": { "newRoot": "$docs" } }
])

考虑到较大的结果集确实具有突破16MB BSON限制的真实可能性,因此这既不是高效的解决方案,也不是安全" .作为黄金法则" ,任何建议将所有内容放入单个文档的数组中的东西:

That is neither a performant solution or "safe" considering that larger result sets run the very real probability of breaching the 16MB BSON limit. As a "golden rule", anything that proposes to put ALL content within the array of a single document:

{ "$group": {
  "_id": null,
  "docs": { "$push": "$$ROOT" }
}}

那是一个基本缺陷,因此不是解决方案.

then that is a basic flaw and therefore not a solution.

处理此问题的更具说服力的方法通常是在结果的运行游标上进行后期处理:

The far more conclusive ways to handle this typically would be post processing on the running cursor of results:

var globalAmount = 0;

db.collection.aggregate([
  { $group: {
    "_id": { 
      y: { $year:"$dateEntry"}, 
      m: { $month:"$dateEntry"}, 
      d: { $dayOfMonth:"$dateEntry"} 
    }, 
    amount: { "$sum": "$amount" }
  }},
  { "$sort": { "_id": 1 } }
]).map(doc => {
  globalAmount += doc.amount;
  return Object.assign(doc, { amount: globalAmount });
})

所以总的来说,这样做总是更好的:

So in general it's always better to:

  • 使用光标迭代和总计的跟踪变量. mapReduce示例是上述简化过程的人为示例.

  • Use cursor iteration and a tracking variable for totals. The mapReduce sample is a contrived example of the simplified process above.

使用预先汇总的总计.可能与光标迭代有关,这取决于您的预聚合过程,而不论是间隔总计还是结转"运行总计.

Use pre-aggregated totals. Possibly in concert with cursor iteration depending on your pre-aggregation process, whether that is just interval total or a "carried forward" running total.

聚合框架应真正用于聚合",仅此而已.通过诸如处理数组之类的过程来强制数据强制处理既不明智也不安全,而且最重要的是,客户端处理代码更简洁,更有效.

The aggregation framework should really be used for "aggregating" and nothing more. Forcing coercions on data via processes like manipulating into an array just to process how you want is neither wise or safe, and most importantly the client manipulation code is far cleaner and more efficient.

让数据库做他们擅长的事情,因为您的操纵"要好得多地用代码来处理.

Let databases do the things they are good at, as you "manipulations" are far better handled in code instead.