简体   繁体   中英

spring data mongodb - aggregation with dates

Following code doesn't give the expected results, i've tried with multiple documents, those documents structured as follows.

_id: 5a7714d44c75220958e6aa01
imei:355227045347655
point: [3.143453333333333,80.10954]
heading: 0
speed:0
timestamp: 2018-02-04 19:42:36.000
point_distance: 525.25

Now i need to calculate sum of point_distance of every record match with given imei and time period. I've tried to achieve this with following code but returns nothing even if required data exists.

public Object findDistance(long imei, Date from, Date to) {
    List aggregationOps = new ArrayList<>();
    //operations
    aggregationOps.add(match(Criteria.where("imei").is(imei)));
    aggregationOps.add(match(Criteria.where("timestamp").gte(from)));
    aggregationOps.add(match(Criteria.where("timestamp").lte(to)));
    aggregationOps.add(group("imei").sum("point_distance").as("distance"));
    aggregationOps.add(project("imei").and("distance").previousOperation());

    AggregationOptions agOps = new AggregationOptions.Builder().allowDiskUse(true).cursor(new BasicDBObject()).build();

    return (DistanceInfo) getMongoTemplate()
            .aggregate(newAggregation(aggregationOps).withOptions(agOps), Location.class, DistanceInfo.class)
            .getUniqueMappedResult();
}

Date > from and to are formatted as yyyy-MM-dd hh:mm:ss

DistanceInfo class

public class DistanceInfo {
    long imei;
    double distance;
}

I'm new to this mongodb stuff and no idea what did i do wrong, how can i correct this ? any help is much appreciated.

Try this. this should work

MatchOperation matchOperation = match(Criteria.where("imei").is(imei) 
     .and("timestamp").gte(from.getTime()).lte(to.getTime()));

GroupOperation groupOperation = group("imei").sum("point_distance").as("distance");
ProjectionOperation projectionOperation = project().andExpression("imei").as("imei")
    .andExpression("distance").as("distance");

Aggregation aggregation = newAggregation(matchOperation, groupOperation, projectionOperation);
AggregationResults<DistanceInfo> results = mongoTemplate.aggregate(aggregation, "location", DistanceInfo.class);

return  results.getMappedResults();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM