【问题标题】:Can't find a codec for class org.springframework.data.mongodb.core.query.GeoCommand找不到类 org.springframework.data.mongodb.core.query.GeoCommand 的编解码器
【发布时间】:2016-04-19 09:15:09
【问题描述】:

这是我的代码

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)

没关系。

我不知道如何处理它。我想在Sphere中获取数据。

【问题讨论】:

    标签: java mongodb spring-boot


    【解决方案1】:

    我使用 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");
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 2019-06-19
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多