Spring 如何在春季编写部分日期/时间(年、月、日、小时)的Mongodb聚合组管道?
我需要帮助在春季编写mongodb聚合管道 下面是我想要执行查询的数据示例(请注意,以下数据是spring中的匹配聚合管道的结果,该聚合管道具有eventDate匹配范围和eventName等于“a”的条件): 现在,我想在eventDate字段上编写组聚合管道,它应该在年、月、日和小时上组,因为我要查找的是小时数据 结果示例我希望它看起来像是仅项目eventDate和eventName字段:Spring 如何在春季编写部分日期/时间(年、月、日、小时)的Mongodb聚合组管道?,spring,mongodb,aggregation-framework,spring-mongo,Spring,Mongodb,Aggregation Framework,Spring Mongo,我需要帮助在春季编写mongodb聚合管道 下面是我想要执行查询的数据示例(请注意,以下数据是spring中的匹配聚合管道的结果,该聚合管道具有eventDate匹配范围和eventName等于“a”的条件): 现在,我想在eventDate字段上编写组聚合管道,它应该在年、月、日和小时上组,因为我要查找的是小时数据 结果示例我希望它看起来像是仅项目eventDate和eventName字段: { "eventDate": ISODate("2017-01-29T03:0
{
"eventDate": ISODate("2017-01-29T03:00:00.000Z"),
"eventName": "A"
}
{
"eventDate": ISODate("2017-01-29T04:00:00.000Z"),
"eventName": "A"
}
因此,如果您注意到上面的数据是以小时为单位的,并且除此之外,分和秒现在设置为0
我尝试了此解决方案,但对我无效:
谢谢你的帮助。提前感谢。您可以在此处使用日期算法,将分、秒和毫秒部分重置为0的日期时间 您可能需要根据spring mongo版本和java版本进行一些调整 版本:Mongo3.2、SpringMongo1.9.5和Java8 Mongo Shell查询:
db.collection.aggregate(
[{
$group: {
_id: {
$subtract: ["$eventDate", {
$add: [{
$multiply: [{
$minute: "$eventDate"
}, 60000]
}, {
$multiply: [{
$second: "$eventDate"
}, 1000]
}, {
$millisecond: "$eventDate"
}]
}]
},
eventName: {
$addToSet: "$eventName"
}
}
}]
)
弹簧代码:
AggregationOperation group = context -> context.getMappedObject(new BasicDBObject(
"$group", new BasicDBObject(
"_id",
new BasicDBObject(
"$subtract",
new Object[]{
"$eventDate",
new BasicDBObject("$add",
new Object[]{
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$minute", "$eventDate"), 60000}),
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$second", "$eventDate"), 1000}),
new BasicDBObject("$millisecond", "$eventDate")
}
)
}
)
).append("eventName", new BasicDBObject("$addToSet", "$eventName"))));
更新:使用$project
db.collection.aggregate(
[{
$project: {
eventDate: {
$subtract: ["$eventDate", {
$add: [{
$multiply: [{
$minute: "$eventDate"
}, 60000]
}, {
$multiply: [{
$second: "$eventDate"
}, 1000]
}, {
$millisecond: "$eventDate"
}]
}]
},
eventName: 1
}
}, {
$group: {
_id: "$eventDate",
eventName: {
$addToSet: "$eventName"
}
}
}]
)
弹簧代码
AggregationOperation project = context -> context.getMappedObject(new BasicDBObject(
"$project", new BasicDBObject(
"eventDate",
new BasicDBObject(
"$subtract",
new Object[]{
"$eventDate",
new BasicDBObject("$add",
new Object[]{
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$minute", "$eventDate"), 60000}),
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$second", "$eventDate"), 1000}),
new BasicDBObject("$millisecond", "$eventDate")
}
)
}
)
).append("eventName", 1)));
AggregationOperation group = Aggregation.group("eventDate").addToSet("eventName").as("eventName");
Aggregation agg = Aggregation.newAggregation(project, group);
如果您不知道,但$subtract不是有效的组运算符,那么您的解决方案将不起作用!For ref:您是否尝试运行代码?它适用于3.2版本。我会找到文件的。我还将更新解决方案,将逻辑移动到
$project
。是的,我尝试运行代码,但它给我一个错误,无效的组运算符$subtract。当然,让我知道!你的mongo版本是什么?我已经更新了mongo shell解决方案,使用$project和spring解决方案。我的项目使用mongo版本3.3.0
AggregationOperation project = context -> context.getMappedObject(new BasicDBObject(
"$project", new BasicDBObject(
"eventDate",
new BasicDBObject(
"$subtract",
new Object[]{
"$eventDate",
new BasicDBObject("$add",
new Object[]{
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$minute", "$eventDate"), 60000}),
new BasicDBObject("$multiply",
new Object[]{new BasicDBObject("$second", "$eventDate"), 1000}),
new BasicDBObject("$millisecond", "$eventDate")
}
)
}
)
).append("eventName", 1)));
AggregationOperation group = Aggregation.group("eventDate").addToSet("eventName").as("eventName");
Aggregation agg = Aggregation.newAggregation(project, group);