Reputation: 397
I want to create an Aggregation query in my spring boot application.
Query:
db.mycollection.aggregate(
{
$match : {
timeStamp: {$gte: ISODate("2020-07-13T00:00:00.000Z"), $lt: ISODate("2020-07-13T23:59:59.000Z")}
}
},
{ "$project": {
"h":{"$hour": { "$add": [ "$timeStamp", 5.5 * 60 * 60 * 1000 ] }}
}},
{ "$group":{
"_id": "$h",
"total":{ "$sum": 1}
}
}
)
I've created following Aggregation object in my spring boot application
Aggregation violationHourlyData = Aggregation.newAggregation(
Aggregation.match(Criteria.where("timeStamp").gte(fromDate).lte(toDate)),
Aggregation.project("_id").andExpression( "hour(timeStamp)").as("h"),
Aggregation.group("h").count().as("count")
);
In the above object, I want to add some time in timeStamp as mentioned in the query.
Thanks
Upvotes: 0
Views: 427
Reputation: 397
Found the answer by understanding andExpression() in detail.
Answer:
Aggregation violationHourlyData = Aggregation.newAggregation(
Aggregation.match(Criteria.where("timeStamp").gte(fromDate).lte(toDate)),
Aggregation.project("_id").andExpression( "hour(add(timeStamp, 5.5 * 60 * 60 * 1000))").as("h"),
Aggregation.group("h").count().as("count")
);
Upvotes: 1