这是我的代码
Criteria c = new Criteria();
c.andOperator(Criteria.where("country").is(country),
Criteria.where("createTime").lte(wp),
Criteria.where("location").withinSphere(circle),
Criteria.where("titleContent").exists(true));
AggregationOperation match = Aggregation.match(c);
AggregationOperation limit = Aggregation.limit(20);
AggregationOperation sort = Aggregation.sort(new Sort(Sort.Direction.DESC, "createTime"));
AggregationOperation group = Aggregation.group("titleContent").sum("count").as("titleCount").count().as("contentCount");
Aggregation aggregation = Aggregation.newAggregation(match, sort, limit, group);
AggregationResults<TitleRes> result = template.aggregate(aggregation, "post", TitleRes.class);
我添加Criteria.where("location").withinSphere(circle)
它将抛出异常
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.GeoCommand
删除Criteria.where("location").withinSphere(circle)
没关系。
我不知道如何处理它。我想获取内部数据。
答案 0 :(得分:0)
我使用geoNear命令解决了这个问题: https://docs.mongodb.com/manual/reference/command/geoNear/
从条件中删除位置并使用geoNear而不是匹配:
NearQuery near = NearQuery.near(new GeoJsonPoint(request.getLongitude(), request.getLatitude()));
near = near.maxDistance(request.getDistance() / Metrics.MILES.getMultiplier());
near = near.num(2000000);//watch out for max num default restriction
near = near.spherical(true);
Query q = new Query();
q.addCriteria(matchCriteria);
near = near.query(q);
AggregationOperation geoNear = Aggregation.geoNear(near, "location");