【问题标题】:java mongodb "geoNear" command return exception error 17304java mongodb“geoNear”命令返回异常错误17304
【发布时间】:2017-09-26 09:46:29
【问题描述】:

我正在使用 mongoDB 在 Java 应用程序中存储和获取与位置相关的信息。

我在使用“geoNear”获取数据时遇到异常...

这是存储数据的代码:

Document doc = new Document(Params.pubId, UUID.fromString(tmsg.pubId))
                                            .append(Params.location, new Point(new Position(tloc.lat, tloc.lon)))
                                            .append(Params.locId, UUID.fromString(tloc.id))
                                            .append(Params.pubTime, tmsg.pubTime)
                                            .append(Params.span, tmsg.span)
                                            .append(Params.messageId, UUID.fromString(tmsg.id))
                                            .append(Params.header, tmsg.header)
                                            .append(Params.senderId, UUID.fromString(tmsg.senderId))
                                            .append(Params.senderDisp, tmsg.name)   
                                            .append(Params.homes, homes)
                                            .append(Params.tags, tags);

collection.insertOne(doc);  

这是获取的代码

Document command = new Document("geoNear",Params.collection).append("near", new Point(new Position(loc.lat, loc.lon)))
                                                                          .append("spherical", true)
                                                                          .append("limit", Params.limit)
                                                                          .append("maxDistance", plan.range)
                                                                          .append("distanceMultiplier", Params.multplierKm);
                //the "geoNear" will always return a non-null object
                Document geoNear = database.runCommand(command);

异常打印出来

com.mongodb.MongoCommandException: Command failed with error 17304: ''near' field must be point' on server data.gubnoi.com:27017. The full response is { "ok" : 0.0, "errmsg" : "'near' field must be point", "code" : 17304, "codeName" : "Location17304" }

这个异常的发生是依赖的,有点奇怪。

在成功案例中,我使用了 0.0, 0.0 的模拟坐标;我确实事先在这个坐标上存储了一些数据。正如我所说的操作成功,我可以成功获取存储的数据。

在失败的情况下,我输入移动终端获取的真实坐标,例如

lat:39.904522 lon: 116.65588

此外,事先没有在该位置存储任何数据。然后以下语句失败并出现异常

Document geoNear = database.runCommand(command);

我在 Java 8 下使用 mongo-java-driver 3.4.1,和 mongodb 3.4.1

请指教

谢谢

【问题讨论】:

    标签: java mongodb geospatial geo


    【解决方案1】:

    我使用了 0.0, 0.0 的模拟坐标;我确实事先在这个坐标上存储了一些数据。正如我所说,手术是成功的。

    尝试颠倒坐标值的顺序; MongoDB 坐标顺序是longitude,然后是latitude。例如:

    new Point(new Position(loc.lon, loc.lat))
    

    它适用于0.00.0,因为这两个值在反向时都是有效的(即无法识别为经纬度)。

    您也可以使用mongo shell 重现此错误:

    > db.runCommand({geoNear: "places",
                     near: { type: "Point", 
                             coordinates: [ 39.904522, 116.65588 ] 
                           },
                     spherical: true    
    });
    
    {
      "ok": 0,
      "errmsg": "'near' field must be point",
      "code": 17304,
      "codeName": "Location17304"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2021-04-15
      相关资源
      最近更新 更多