2

I am using aggregation as following :

    final List<AggregationOperation> aggregations = new ArrayList<>();
    Polygon polygon = new Polygon(new Point(-26.28125, 42.19231862526141), new Point(100.28125, 64.7157757187955),
            new Point(100.28125, 42.19231862526141), new Point(-26.28125, 64.7157757187955));
    AggregationOperation match = new MatchOperation(Criteria.where("location").within(polygon));
    aggregationOperations.add(match);
    aggregations.add(project("_id", "location","distance",User.COLLECTION_NAME)
            .and("$geoHash").substring(0,slice).as("geo"));
    aggregations.add(group("geo").count().as("count")
            .avg("location.lng").as("lon")
            .avg("location.lat").as("lat")
            .first(User.COLLECTION_NAME).as(User.COLLECTION_NAME));
    final Aggregation aggregation = newAggregation(aggregations);
    AggregationResults<ClusteredLocation> groupResults =
            mongoTemplate.aggregate(aggregation, UserLocation.COLLECTION_NAME, ClusteredLocation.class);
    return groupResults.getMappedResults();

Aggregation which is being created is as follows: { "aggregate" : "collection", "pipeline" : [ { "$match" : { "location" : { "$geoWithin" : { "$java" : org.springframework.data.mongodb.core.query.GeoCommand@d502fd15 } } } }, { "$lookup" : { "from" : "users", "localField" : "_id", "foreignField" : "_id", "as" : "users" } }, { "$project" : { "_id" : 1, "location" : 1, "distance" : 1, "users" : 1, "geo" : { "$substr" : ["$geoHash", 0, 3] } } }, { "$group" : { "_id" : "$geo", "count" : { "$sum" : 1 }, "lon" : { "$avg" : "$location.lng" }, "lat" : { "$avg" : "$location.lat" }, "users" : { "$first" : "$users" } } } ] }

Exception I am getting as follows:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.GeoCommand.

Am I doing anything wrong in match operation?

1
  • 1
    This pretty much looks like a bug. As it seems the Criteria is not run through the QueryMapper passing a non MongoDB simple type to the driver. I've created DATAMONGO-1986 to investigate what happens here. Commented May 23, 2018 at 11:39

1 Answer 1

0

You can workaround the issue by using a TypedAggregation or by explicitly providing an input type. Both strategies enforce the query mapping within the template.

TypedAggregation agg = newAggregation(UserLocation.class, aggregations);
mongoTemplate.aggregate(agg, COLLECTION_NAME, ClusteredLocation.class);

// or

Aggregation agg = newAggregation(aggregations);
mongoTemplate.aggregate(agg, UserLocation.class, COLLECTION_NAME, ClusteredLocation.class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.