MongoDB填坑Can't find a codec for class org.springframework.data.mongodb.core.query.GeoCommand

之前我写过一篇文章:MongoDB LBS经纬度查询操作,我再后续的实战中发现了一些坑,再次帮大家填一下!

Geo字段顺序问题

在这里插入图片描述

注意一下locationlng(经度),lat(纬度),按照这个顺序查询时没问题的,但是如果库中有的是lat纬度在前,那么,就会出现两个问题

  • 无法建立2dsphere索引。(需要数据清洗)
  • 建立2d索引后查询不出正确的数据。

对于问题二,无法查询正确的结果,在不做数据清洗的情况下,可以使用管道查询来解决,代码如下:

db.lagou_job.aggregate([
    {$project:{"location.lng" : "$location.lng","location.lat" : "$location.lat" },
    {$match : {"location" : { "$geoWithin" : { "$box" : [[116.308603,39.989283] ,[116.351793,39.972033]] } } }
    }
])

主要是将lnglat重新命名,修改顺序后,在使用geoWithin查询。

Spring-driver-mongodb

上面sql对应的java代码如下:

		Field lngField = Fields.field("location.lng", "location.lng");
        Field latField = Fields.field("location.lat", "location.lat");
        AggregationOperation project = new ProjectionOperation(Fields.from(lngField, latField));

        double[] box1 = {116.308603,39.989283};
        double[] box2 = {116.351793,39.972033};
        // 矩形
        Box shape = new Box(box1, box2);

        AggregationOperation boxMatch = new MatchOperation(Criteria.where("location").within(shape));
        AggregationOperation skip = new SkipOperation(0);

        AggregationOperation limit = new LimitOperation(15);
        Aggregation agg = newAggregation(match, project, boxMatch, skip, limit);
        List<Result> resultList = mongoTemplate.aggregate(agg,
                "company",
                Result.class).getMappedResults();

出现问题的spring-boot版本:

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

最后执行的mongo语句为:

{ "aggregate" : "company", "pipeline" : 
[ { "$project" : { "location.lng" : 1, "location.lat" : 1} }, { "$match" : { "location" : { "$geoWithin" : { "$java" : org.springframework.data.mongodb.core.query.GeoCommand@703ee59 } } } }, { "$skip" : { "$numberLong" : "0" } }, { "$limit" : { "$numberLong" : "15" } } ]
}

其实从这个输出的语句就能看到明显的错误了:

"$java" : org.springframework.data.mongodb.core.query.GeoCommand@703ee59 

错误输出:

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

问题解决:

参考文章: https://stackoverflow.com/questions/50474452/org-bson-codecs-configuration-codecconfigurationexception-cant-find-a-codec-fo

这篇文章中的哥们,也碰到了同样的问题,之后像spring提出了一个bug:spring-jira-1986
很快spring也解决了这个问题:github-spring-data-mongodb-pull-564

那么具体到我这个项目上,怎么办呢?在这里插入图片描述

可以看到这个问题是spring-boot-starter-parent在2.1.0版本中解决的,so,我们来愉(忐)快(忑)的升级spring-boot的版本吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qnloft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值