【发布时间】: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