我的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
}
}
}
这里是insert,声明:
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我可以获得每天订单的数量,但是我不确定如何管道结果并按货币拆分,然后将结果总计?
任何建议非常感谢
答案 0 :(得分:2)
您可以尝试以下聚合管道,按年 - 月 - 日和货币对文档进行分组,以获得所需的结果:
ImageView
示例输出
TextView