我正在使用聚合如下:
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();
正在创建的聚合如下: {&#34;聚合&#34; :&#34; 集合&#34;,&#34;管道&#34; :[{&#34; $ match&#34; :{&#34; location&#34; :{&#34; $ geoWithin&#34; :{&#34; $ java&#34; :org.springframework.data.mongodb.core.query.GeoCommand@d502fd15}}}},{&#34; $ lookup&#34; :{&#34;来自&#34; :&#34;用户&#34;,&#34; localField&#34; :&#34; _id&#34;,&#34; foreignField&#34; :&#34; _id&#34;,&#34; as&#34; :&#34;用户&#34; },{&#34; $ project&#34; :{&#34; _id&#34; :1,&#34; location&#34; :1,&#34;距离&#34; :1,&#34;用户&#34; :1,&#34; geo&#34; :{&#34; $ substr&#34; :[&#34; $ geoHash&#34;,0,3]}}},{&#34; $ group&#34; :{&#34; _id&#34; :&#34; $ geo&#34;,&#34; count&#34; :{&#34; $ sum&#34; :1},&#34; lon&#34; :{&#34; $ avg&#34; :&#34; $ location.lng&#34; },&#34; lat&#34; :{&#34; $ avg&#34; :&#34; $ location.lat&#34; },&#34;用户&#34; :{&#34; $ first&#34; :&#34; $ users&#34; }}}}}
例外我得到如下:
org.bson.codecs.configuration.CodecConfigurationException:找不到类org.springframework.data.mongodb.core.query.GeoCommand的编解码器。
我在比赛中做错了吗?
答案 0 :(得分:0)
您可以使用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);