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

标签 mongodb spring-data-mongodb

我正在使用如下聚合:

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

正在创建的聚合如下: { “聚合”:“集合”,“管道”:[ { “$match”:{ “位置”:{ “$geoWithin”:{ “$java”:org.springframework.data。 mongodb.core.query.GeoCommand@d502fd15 } } } }, { "$lookup": { "from": "users", "localField": "_id", "foreignField": "_id", "as": "用户”}},{“$project”:{“_id”:1,“位置”:1,“距离”:1,“用户”:1,“geo”:{“$substr”:[“$geoHash ", 0, 3] } } }, { "$group": { "_id": "$geo", "count": { "$sum": 1 }, "lon": { "$avg": "$location.lng"}, "lat": { "$avg": "$location.lat"}, "users": { "$first": "$users"} } } ] }

异常如下:

org.bson.codecs.configuration.CodecConfigurationException:找不到类 org.springframework.data.mongodb.core.query.GeoCommand 的编解码器。

我在匹配操作中做错了什么吗?

最佳答案

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

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

关于mongodb - org.bson.codecs.configuration.CodecConfigurationException : Can't find a codec for class org. springframework.data.mongodb.core.query.GeoCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474452/

相关文章:

MongoDB:使用嵌入数组的大小更新字段

java - 使用 spring 数据从 mongodb 中选择随机条目

java - Spring MVC MongoDB 配置

java - Spring 如何在 mongo 中构建它的 like* 查询?

java - MongoTemplate 拉取查询

java - Spring 数据蒙戈: Find by array intersection

mongodb - 每个用户的集合或用户的集合

javascript - 一些页面的一个菜单,带有来自 mongodb 的链接

MongoDb $lookup 对每行的外部集合进行读取

linux - 为什么mongodb提示transparent_hugepage?