【问题标题】:group by date and then by currecny and sum mongo records按日期分组,然后按货币和总和 mongodb 记录
【发布时间】:2015-09-15 20:32:59
【问题描述】:

我的 mongo 数据库中有以下记录

> db.orders.find({});
{
    "_id" : "WEB3",
    "currency" : "USD",
    "company" : "b",
    "user_id" : "b",
    "timestamp" : ISODate("2015-06-26T12:13:18.570Z"),
    "details" : {
        "ordered" : {
            "total" : 1910.4,
            "deliveryVAT" : 120,
            "delivery" : 600,
            "goodsVAT" : 198.4,
            "goodsTotal" : 992
        }
    }
}
{
    "_id" : "WEB1",
    "currency" : "GBP",
    "company" : "a",
    "user_id" : "a",
    "timestamp" : ISODate("2015-06-26T12:11:08.570Z"),
    "details" : {
        "ordered" : {
            "total" : 1910.4,
            "deliveryVAT" : 120,
            "delivery" : 600,
            "goodsVAT" : 198.4,
            "goodsTotal" : 992
        }
    }
}
{
    "_id" : "WEB2",
    "currency" : "GBP",
    "company" : "a",
    "user_id" : "a",
    "timestamp" : ISODate("2015-06-26T12:11:18.570Z"),
    "details" : {
        "ordered" : {
            "total" : 1910.4,
            "deliveryVAT" : 120,
            "delivery" : 600,
            "goodsVAT" : 198.4,
            "goodsTotal" : 992
        }
    }
}

这里是插入语句:

db.orders.insert( { "_id" : "WEB1", "currency" : "GBP", "company" : "a", "user_id" : "a", "timestamp" : ISODate("2015-06-26T12:11:08.570Z"), "details" : { "ordered" : { "total" : 1910.4, "deliveryVAT" : 120, "delivery" : 600, "goodsVAT" : 198.4, "goodsTotal" : 992 } } });
db.orders.insert( { "_id" : "WEB2", "currency" : "GBP", "company" : "a", "user_id" : "a", "timestamp" : ISODate("2015-06-26T12:11:18.570Z"), "details" : { "ordered" : { "total" : 1910.4, "deliveryVAT" : 120, "delivery" : 600, "goodsVAT" : 198.4, "goodsTotal" : 992 } } });
db.orders.insert( { "_id" : "WEB3", "currency" : "USD", "company" : "b", "user_id" : "b", "timestamp" : ISODate("2015-06-26T12:13:18.570Z"), "details" : { "ordered" : { "total" : 1910.4, "deliveryVAT" : 120, "delivery" : 600, "goodsVAT" : 198.4, "goodsTotal" : 992 } } });

按日期分组然后按货币分组的正确方法是什么,这样我得到的结果类似于:

{ [ "date": "2015-06-26",
  "currency": "USD",
  "total": 1910.4,
  "no_of_orders": 1],
  [ "date": "2015-06-26",
  "currency": "GBP",
  "total": 3820.8,
  "no_of_orders": 2]]
}

从以前的帖子mongodb sort result of aggregate query and display day name 我可以得到每天的订单数量,但是我不确定如何通过管道传输结果并按货币拆分,然后汇总结果?

非常感谢任何建议

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以尝试以下聚合管道,该管道按年-月-日和货币对文档进行分组以获得所需的结果:

    db.orders.aggregate([
        { 
            "$group": { 
                "_id": {
                    "day": { "$dayOfMonth" : "$timestamp" },
                    "month": { "$month" : "$timestamp" },
                    "year": { "$year" : "$timestamp" },
                    "currency": "$currency"
                }, 
                "total": { "$sum": "$details.ordered.total" },
                "no_of_orders": { "$sum": 1 }
            } 
        },
        { 
            "$project": { 
                "_id": 0, 
                "dateDay": {
                    "$concat": [ 
                        {"$substr" : [ "$_id.day", 0, 2]}, "-",
                        {"$substr" : [ "$_id.month", 0, 2]}, "-",
                        {"$substr" : [ "$_id.year", 0, 4]}
                    ] 
                },            
                "total": 1,
                "no_of_orders": 1,
                "currency": "$_id.currency"
            }
    
        }
    ])
    

    样本输出

    /* 0 */
    {
        "result" : [ 
            {
                "total" : 1910.4,
                "no_of_orders" : 1,
                "dateDay" : "26-6-2015",
                "currency" : "USD"
            }, 
            {
                "total" : 3820.8,
                "no_of_orders" : 2,
                "dateDay" : "26-6-2015",
                "currency" : "GBP"
            }
        ],
        "ok" : 1
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 2016-03-23
      • 2019-08-09
      • 2023-03-04
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多