0

What could be variation of this aggregation to get the result or items where the difference of the days = (a number) and the $acceptedDate is greater than $liveDate - x days

    AggregationOperation redact = new AggregationOperation() {
        @Override
        public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("if",  BasicDBObject.parse("{'$gte':[{'$subtract':[{'$ifNull':['$acceptedDate', 
{'$date':" + System.currentTimeMillis() + "}]},'$lastVisit' **here minus x days **]},1296000000]}}")); //and the difference is 10 days
        map.put("then", "$$KEEP");
        map.put("else", "$$PRUNE");
        return new BasicDBObject("$redact", new BasicDBObject("$cond", map));
    };

    Aggregation aggregation = Aggregation.newAggregation(redact);

    List<FactoryAcceptance> results = mongoTemplate.aggregate(aggregation, FactoryAcceptance.class, FactoryAcceptance.class).getMappedResults();

Basically a parameter is being passed as a variation for this where they want to match the difference in days (say show me records where the difference is 5 days), but in this case lastVisit should be minus 5 days.

jedgard
  • 868
  • 3
  • 23
  • 41

1 Answers1

1

You can use below query.

BasicDBObject.parse("
  {'$eq':[
     {'$subtract':[
       {'$ifNull':['$acceptedDate',{'$date':" + System.currentTimeMillis() + "}]},
       {'$subtract':['$lastVisit', 216000000]} // 5 days
     ]}, 
     432000000 // 10 days
  ]}"
);
s7vr
  • 73,656
  • 11
  • 106
  • 127