0
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}
       
    }
   
]

)

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • I think it should be rather `$lte: ISODate("2020-11-29T23:59:59Z")` or `$lt: ISODate("2020-11-30T00:00:00Z")` – Wernfried Domscheit Dec 01 '20 at 20:25
  • Hey really thanks, the query for console works fine tho, really is more about the first block in java, I'm using latest of mongodb and spring boot, forgot to mention that. – sd.3391519 Dec 02 '20 at 14:04

0 Answers0