db.billingDomain.aggregate([
{
$group: {
_id: {
day: { $dayOfMonth: "$createdAt" },
month: { $month: "$createdAt" },
year: { $year: "$createdAt" }
},
count: {
$sum: 1
},
date: {
$first: "$createdAt"
}
}
},
{
$project: {
date: {
$dateToString: {
format: "%Y-%m-%d",
date: "$date"
}
},
count: 1,
_id: 0
}
},
{
$match: {
"date": {
$gte: "2020-06-05"
}
}
}
])
如何使用mongo模板aggregation在springjava中构建这个查询?面临进行此查询的挑战
1条答案
按热度按时间eulz3vhy1#
并非所有mongodb语法都受
Spring Mongo
,所以我们需要实施AggregationOperation
手动(java>=v1.8
):编辑:
System.out.println(agg);
```{
"aggregate":"collection",
"pipeline":[
{
"$group":{
"_id":{
"day":{
"$dayOfMonth":"$createdAt"
},
"month":{
"$month":"$createdAt"
},
"year":{
"$year":"$createdAt"
}
},
"count":{
"$sum":1
},
"date":{
"$first":"$createdAt"
}
}
},
{
"$project":{
"count":1,
"_id":0,
"date":{
"$dateToString":{
"format":"%Y-%m-%d",
"date":"$date"
}
}
}
},
{
"$match":{
"date":{
"$gte":"2020-06-05"
}
}
}
]
}