使用一个 MongoDB group() 查询为该月的每一天生成一组总和

发布于 2024-11-17 01:42:23 字数 652 浏览 6 评论 0原文

我创建了一个 MongoDB 聚合查询,它将给出当月的数据总和,但是我如何修改它,以便它从该月的每一天返回一个数组,以及每天的总数(我假设这是可能的,但我发现很难让它发挥作用)?如果这不可能,是否有比使用循环并运行 30 个组查询更好的方法?

我正在使用 PHP 驱动程序,但 shell 中的答案同样有用。

$total_this_month = $db->test->group(
    array(  ),
    array(
        'sum' => 0
    ),
    new MongoCode( 'function(doc, out){ out.sum += doc.data; }' ),
    array(
        'condition' => array(
            'time' => array(
                '$gte' => new MongoDate(strtotime('first day of this month, 00:00:00')),
                '$lte' => new MongoDate(strtotime('last day of this month, 23:59:59'))
            )
        )
    )
);

I've created a MongoDB aggregation query that will give me the sum of data for the current month, but how could I modify this so that it would return an array from each day of the month, with the total for each day (I assume this is possible, but I'm finding it difficult to get it working)? If this isn't possible, is there a better way of doing this than using a loop and running 30 group queries?

I'm using the PHP driver, but an answer in shell is just as useful.

$total_this_month = $db->test->group(
    array(  ),
    array(
        'sum' => 0
    ),
    new MongoCode( 'function(doc, out){ out.sum += doc.data; }' ),
    array(
        'condition' => array(
            'time' => array(
                '$gte' => new MongoDate(strtotime('first day of this month, 00:00:00')),
                '$lte' => new MongoDate(strtotime('last day of this month, 23:59:59'))
            )
        )
    )
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风不识月 2024-11-24 01:42:23

如果您计划经常运行组查询,则应考虑添加一个或多个新字段,以便您可以按所需的时间段进行分组。如果这是一个不需要性能调整的临时查询,那么使用 Map-Reduce 的前一个答案是一个很好的答案。例如,我有一个集合,有时需要按天、周、月等进行聚合。这是一个示例记录:

{"_id" : ObjectId("4ddaed3a8b0f766963000003"),
 "name": "Sample Data",
 "time" : "Mon May 23 2011 17:26:50 GMT-0600 (MDT)",
 "period" : {
   "m" : 201105,
   "w" : 201121,
   "d" : 20110523,
   "h" : 2011052317
 }
}

通过这些附加字段,我可以使用组函数执行更多操作,还可以为这些字段建立索引以加快速度查询。您可以像我一样选择使用整数,也可以选择字符串 - 任何一种方式都可以,但请记住您的查询参数需要具有相同的数据类型。我喜欢整数,因为它们似乎应该表现得更好一点并且使用更少的空间(只是预感)。

If you plan on running your group query often you should consider adding a new field or fields that allow you to group by the time period you need. The previous answer of using map-reduce is a great one if this is an ad-hoc query that doesn't need performance tuning. For example, I have a collection that needs to be aggregated sometimes by day, week, month, etc. Here is an example record:

{"_id" : ObjectId("4ddaed3a8b0f766963000003"),
 "name": "Sample Data",
 "time" : "Mon May 23 2011 17:26:50 GMT-0600 (MDT)",
 "period" : {
   "m" : 201105,
   "w" : 201121,
   "d" : 20110523,
   "h" : 2011052317
 }
}

With these additional fields I can do a lot more with the group function and can also index those fields for faster queries. You can choose to use integers, as I did, or strings - either way will work, but remember that your query parameters need to be of the same data type. I like integers because it seems that they should perform a little better and use less space (just a hunch).

吹梦到西洲 2024-11-24 01:42:23

group 命令不支持通过函数生成新的分组键,因此请使用 map/reduce 而不是 group。

The group command doesn't support generating new grouping keys via a function, so use map/reduce instead of group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文