提问者:小点点

CodecConfigurationException:找不到类org.springframework.data编解码器


我使用聚合如下:

    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();

正在创建的聚合如下:{"聚合":"集合","管道" : [ { "$匹配" : { "位置" : { "$GeoIn" : { "$java":org.springframework.data.mongodb.core.query.GeoCommand@d502fd15 } } } }, { "$查找" : { "from":"用户","localField":"_id","外域":"_id","as":"用户" } }, { "$项目" : { "_id": 1,"位置":1,"距离":1,"用户":1,"geo" : { "$substr" : ["$GeoHash",0,3] } } }, { "$组" : { "_id":"$geo","count":{"$sum": 1},"lon" : { "$avg":"$location.lng"},"lat" : { "$avg":"$location.lat"},"用户" : { "$第一":"$用户" } } } ] }

例外我得到如下:

CodecConfigurationException:找不到类org.springframework.data编解码器。

我在比赛操作中做错了什么吗?


共1个答案

匿名用户

您可以通过使用TyedAgpse或显式提供输入类型来解决问题。这两种策略都在模板中强制执行查询映射。

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);