Aggregation aggregation = newAggregation(
match(Criteria.where("moment").gte(from).lte(to)),
project("pass", "carrier", "route", "iso").and("iso").as("country")
.and("$moment").plus(14400000).as("shifted"),
project("shifted").and(DateOperators.DayOfMonth.dayOfMonth("$moment")).previousOperation(),
.andExpression("$moment").dateAsFormattedString("%Y-%m-%d").as("date"),
group("pass", "carrier", "route", "country","date").count().as("total"),
sort(Sort.Direction.DESC, "pass","total")
).withOptions(newAggregationOptions().
allowDiskUse(true).build());
I am trying to do this query in java (mongodb and spring boot) that works in console (the bottom one works fine) but i failed to pass the correct date shifted already (called shifted) to the DateOperators.DayOfMonth. I get projection field must not be null so I think i am using wrong previousOperation(). How should i properly use this previousOperation? or how can i correctly shifted the date i need? thank you very much in advance.
db.billing.aggregate(
[
{
$match: { "moment": { "$gte": ISODate("2020-11-29T00:00:00Z"), "$lt": ISODate("2020-11-29T23:59:59Z")}}
},
{
$project: { "pass" :1, "carrier": 1, "moment" : { $subtract: ["$moment", 14400000 ] } }
},
{
$group: { "_id": {"pass": "$pass","carrier": "$carrier", "day": { "$dayOfMonth": "$moment"},"total": { "$sum": 1 }}}
},
{
$sort: {"_id.day":1}
}
]
)