我想使用spring data mongodb创建一个聚合查询。
我的收藏数据结构是:
{
date: ISODate("2022-12-11T01:13:00.000Z"),
metadata: {
homeId: '51b87ea3d846f450141ae58z',
nodeId: '51b87ea3d846f450141ae58z'
},
value: 42,
_id: ObjectId("63b583e30c1e523313b64ed5")
}
我想翻译成java的MongoDb查询是:
db.consumptions.aggregate(
[
{ $match : {"metadata.nodeId": "51b87ea3d846f450141ae58z"}
},
{ $project: {
date: {
$dateToParts: { date: "$date"}
},
value: 1
}
},
{ $group: {
_id: {
date: {
year: "$date.year", month: "$date.month", day: "$date.day"
}
},
cumulValue: { $sum: "$value"}
}
},
{ $sort : { "_id.date": -1}
}
]
)
MongoDb查询结果如下所示:
[
{
_id: {
date: { year: 2022, month: 12, day: 11 }
},
cumulValue: 42
}
]
正如您在这里看到的,结果结构是**_id**,其中包含一个嵌套对象date,该对象包含字段year、month和day。
我希望使用groupOperation和spring data mongo得到相同的result _id结构。
我尝试了这个实现
MatchOperation matchOperation = Aggregation.match(
new Criteria().andOperator(Criteria.where("metadata.nodeId").is(nodeId);
// projection Operation
ProjectionOperation projectionOperation = project("value")
.and(DateOperators.dateOf("date").toParts()).as("date");
// group Operation
GroupOperation groupOperation = Aggregation.group(
"date.year", "date.month", "date.day")
.sum("value").as("cumulativeValue");
// Sort Operation
SortOperation sortOperation = sort(pageRequest.getSort());
Aggregation aggregation = Aggregation.newAggregation(
matchOperation, projectionOperation, groupOperation,sortOperation);
但是我不能得到预期的结果结构(带有嵌套对象日期)
{
_id: {
year: 2022,
month: 11,
day: 11
},
cumulValue: 284
}
1条答案
按热度按时间mnemlml81#
您可以通过分析整个管道字符串来尝试此操作。