基于过去日期的Mongodb聚合
Mongodb aggregate based on past date
我正试图在Mongodb中执行一个查询。我想执行的查询是根据日期(过去7天)查找集合中的所有订单,然后将每个订单的价格与嵌套对象相加。到目前为止,我有以下代码:
采集/数据
{
"_id" : "g32fYpydfSFDbFkoi",
"orderNumber" : 1234,
"createdAt" : ISODate("2016-01-12T13:50:17.559Z"),
"productsInOrder" : [
{
"category" : "ambient",
"item" : 23982,
"desc" : "Ergonomic Cotton Sausages",
"quantity" : "456",
"price" : "0.54",
"lineprice" : "246.24",
"_id" : "BdD4QnM7sYTwBpLds"
},
{
"category" : "ambient",
"item" : 15336,
"desc" : "Rustic Wooden Chicken",
"quantity" : "2",
"price" : "1.87",
"lineprice" : "3.74",
"_id" : "PvtSxi2MfYrZNTD6f"
},
{
"category" : "chilled",
"item" : 57584,
"desc" : "Unbranded Soft Chicken",
"quantity" : "3",
"price" : "4.69",
"lineprice" : "14.07",
"_id" : "ppkECqmhPvg7pQcgB"
},
{
"category" : "ambient",
"item" : 71168,
"desc" : "Rustic Rubber Computer",
"quantity" : "5",
"price" : "3.04",
"lineprice" : "15.20",
"_id" : "bZtr5dkvsG92YtLoe"
},
{
"category" : "frozen",
"item" : 87431,
"desc" : "Unbranded Granite Sausages",
"quantity" : "5678",
"price" : "1.98",
"lineprice" : "11242.44",
"_id" : "ZKur3rHhtCLsWiENr"
},
{
"category" : "frozen",
"item" : 75007,
"desc" : "Practical Frozen Towels",
"quantity" : "678",
"price" : "1.19",
"lineprice" : "806.82",
"_id" : "g78zvzoE8wJkciD9C"
},
{
"category" : "frozen",
"item" : 84721,
"desc" : "Fantastic Metal Hat",
"quantity" : "34",
"price" : "1.83",
"lineprice" : "62.22",
"_id" : "4aqxBWhXy5cabbbiM"
},
{
"category" : "frozen",
"item" : 72240,
"desc" : "Fantastic Granite Towels",
"quantity" : "1",
"price" : "2.94",
"lineprice" : "2.94",
"_id" : "MQD2LNv36mE3BWvZJ"
},
{
"category" : "chilled",
"item" : 89448,
"desc" : "Intelligent Concrete Towels",
"quantity" : "6678",
"price" : "0.42",
"lineprice" : "2804.76",
"_id" : "AjRrxFT4mfpxuciC4"
},
{
"category" : "chilled",
"item" : 57584,
"desc" : "Unbranded Soft Chicken",
"quantity" : "1111",
"price" : "4.69",
"lineprice" : "5210.59",
"_id" : "4yBspve6mBNNzqDnZ"
}
]
}
查询
Orders.aggregate([
{ $match: { 'createdAt': { $gt: pastDate }}},
{ $unwind: '$productsInOrder' },
{
$group: {
_id: null,
price: {
$sum: '$productsInOrder.price'
}
}
}
]);
我最终想要的是输出过去7天每天的总价。有人能帮我指明正确的方向吗?非常感谢。
首先,$sum
运算符将忽略非数字值,而productsInOrder.price
子文档字段为String类型,因此最好将其转换为数字字段。
完成此操作后,要输出过去7天内每天的总价,请按键更改组以使用$dayOfMonth
运算符,该运算符在7天范围内每天对文档进行分组,如以下所示
Orders.aggregate([
{ "$match": { "createdAt": { "$gt": pastDate } } },
{ "$unwind": "$productsInOrder" },
{
"$group": {
"_id": {
"day": { "$dayOfMonth": "$createdAt" }
},
"price": { "$sum": "$productsInOrder.price" }
}
}
]);
相关文章:
- Mongodb/JS:查找最短(最早)日期
- 如何访问日期并将其插入数据库?节点.JS&MongoDB
- Mongodb,按日期差异分组并获取小时
- 将JSON插入MongoDB中,自动转换字符串中的日期
- mongodb mongodump json日期字段解析错误
- 更改MongoDB中日期过滤器的灵敏度
- 转换日期MongoDB/Javascript,但Nan
- mongoDB动态查询中的日期筛选器
- Meteor:如何使用Momentjs在Mongodb中添加和查询日期
- MongoDB中的日期字段类型(并对其进行排序)
- MongoDB:将Days字段添加到DB中的Date Type字段中,然后与当前日期进行比较
- MongoDb 在存储为字符串时按日期查找
- 将日期存储在MongoDB中,而不考虑时区
- 在MongoDB中保存和排序日期
- MongoDB日期对象在转换为字符串时返回EDT
- MongoDB ObjectID 和日期提取
- 如何在 mongoDB 中正确增加许多日期
- 如何将像 dd-mm-yyyy 这样的日期转换为 ISO 日期格式,以存储在 mongoDB 中
- 用angularJS和输入类型=“”更新mongodb中的日期;日期”;
- 基于过去日期的Mongodb聚合