gpt4 book ai didi

node.js - MongoDB 按日期聚合

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:55 25 4
gpt4 key购买 nike

这是我的收藏的样子...

{
"_id" : ObjectId("53c66071409aeb38133c6ece"),
"starttime" : ISODate("2014-07-15T11:22:25Z"),
"product" : "1",
"customer" : "Microsoft"
}
{
"_id" : ObjectId("53c66071409aeb38133c6ecf"),
"starttime" : ISODate("2014-07-15T11:22:25.882Z"),
"customer" : "Microsoft",
}

{
"_id" : ObjectId("53c66072409aeb38133c6ed0"),
"starttime" : ISODate("2014-07-16T11:22:26.15Z"),
"customer" : "Microsoft",
}

{
"_id" : ObjectId("53c66072409aeb38133c6ed0"),
"starttime" : ISODate("2014-07-16T11:22:27.15Z"),
"customer" : "Apple",
}

我想按日期对数据进行分组并格式化输出,使其显示如下...(我对我缺少的内容发表了评论)

[
{
"_id": {
"date": {
"year": 2014,
"month": 7,
"day": 16
}
},
"productions": [
{
"name": "Microsoft",
"number": 1 //THIS IS MISSING AT THE MOMENT
},
{
"name": "Apple",
"number": 1 //THIS IS MISSING AT THE MOMENT
}
],
"total": 2
},
{
"_id": {
"date": {
"year": 2014,
"month": 7,
"day": 15
}
},
"productions": [
{
"name": "Microsoft",
"number": 2 //THIS IS MISSING AT THE MOMENT
}
],
"total": 2
}
]

我遵循了问题groups by month and year using mongoose.js的答案我已经很接近解决这个问题了,但还没有完全解决。我现在已经到了只见树木不见森林的地步了!

这是我到目前为止的代码...

Job.aggregate({
$project: {
date: {
year: {
$year: "$starttime",
},
month: {
$month: "$starttime"
},
day: {
$dayOfMonth: "$starttime"
}
},
customer: "$customer",
}
}, {
$group: {
_id: {
date: {
year: "$date.year",
month: "$date.month",
day: "$date.day"
},
},
productions: {
$addToSet: {
name: "$customer",
},
},
total: {
$sum: 1
}
},
},
function(error, customers) {
if (error) res.json(error);
res.json(customers);
})

缺少的是每个日期的“制作数量”总数。我刚刚得到日期总数。有人能告诉我哪里出错了吗?我将非常感激。谢谢。

最佳答案

为了实现您想要的目标,您的聚合管道中需要 2 个 $group 阶段。

db.Job.aggregate([
{"$project" : {"date":{"year":{"$year":"$starttime"},"month":{"$month":"$starttime"},"day":{"$dayOfMonth":"$starttime"}},"customer":"$customer"}},
// Group by "customer" as well
{"$group" : {
"_id":{"date":{"year":"$date.year","month":"$date.month","day":"$date.day",customer:"$customer"}},
"total":{"$sum":1}
}},
// Adding a $project phase just to simplify the JSON
{"$project" : {year:"$_id.date.year", month:"$_id.date.month", day:"$_id.date.day", customer:"$_id.date.customer", total:"$total", _id:0}},
// Second group phase to get the desired output
{"$group" : {
_id:{year:"$year", month:"$month", day:"$day"},
productions:{$addToSet:{name:"$customer", number:"$total"}},
total:{$sum:"$total"}
}}
])

上面的查询产生输出:

{
"result" : [
{
"_id" : {
"year" : 2014,
"month" : 7,
"day" : 15
},
"productions" : [
{
"name" : "Microsoft",
"number" : 2
}
],
"total" : 2
},
{
"_id" : {
"year" : 2014,
"month" : 7,
"day" : 16
},
"productions" : [
{
"name" : "Microsoft",
"number" : 1
},
{
"name" : "Apple",
"number" : 1
}
],
"total" : 2
}
],
"ok" : 1
}

为了更好地了解正在发生的情况,请查看管道每个阶段的输出。

关于node.js - MongoDB 按日期聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com