- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在集合 offers
中有以下示例 mongodb 文档。
{
"countryCode" : "LU",
"geoLocation" : [
{
"lat" : 49.8914114000000026,
"lng" : 6.0950994999999999
}
]
},
{
"countryCode" : "DE",
"geoLocation" : [
{
"lat" : 50.8218536999999984,
"lng" : 9.0202151000000015
}
]
}
根据半径50公里左右的地理位置获取文档
db.getCollection('offers').aggregate([
{
$geoNear: {
near: [6.0950994999999999, 49.8914114000000026],
distanceField: "geoLocation",
maxDistance: 50000, //50 km radius
}
}
]);
它正在获取所有文档,而不是搜索大约 50 公里。
任何建议将不胜感激。
最佳答案
旧版坐标对不返回以米为单位的距离,而是以弧度为单位。因此,您的 maxDistance 远大于 50km。参见 Calculate Distances with Spherical Geometry
但也要考虑这里使用的实际距离:
db.getCollection('offers').aggregate([
{
$geoNear: {
near: [6.0950994999999999, 49.8914114000000026],
distanceField: "distance"
}
]);
返回:
{
"_id" : ObjectId("55dc0f4b6f07ad5d3ec3b6d0"),
"countryCode" : "DE",
"geoLocation" : [
{
"lat" : 50.8218537,
"lng" : 9.020215100000001
}
],
"distance" : 60.588259822017925
}
{
"_id" : ObjectId("55dc0f4b6f07ad5d3ec3b6cf"),
"countryCode" : "LU",
"geoLocation" : [
{
"lat" : 49.8914114,
"lng" : 6.0950995
}
],
"distance" : 61.93733827090219
}
因为“distanceField”是报告距查询点的投影距离的字段。
因此,起点是您的文档对于地理空间查询无效。像这样修复它们:
首先删除所有索引:
db.offers.dropIndexes();
然后修复字段:
var bulk = db.offers.initializeOrderedBulkOp(),
count = 0;
db.offers.find({}).forEach(function(doc) {
//printjson(doc);
var tmp = {
"type": "Point",
"coordinates": [doc.geoLocation[0].lng,doc.geoLocation[0].lat]
};
doc.geoLocation = tmp;
printjson(doc);
bulk.find({ "_id": doc._id }).updateOne({
"$set": { "geoLocation": doc.geoLocation }
});
count++;
if ( count % 1000 == 0 ) {
bulk.execute();
bulk = db.offers.initializeOrderedBulkOp();
}
});
现在数据以正确的 GeoJSON 格式表示:
{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d1"),
"countryCode" : "LU",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
6.0950995,
49.8914114
]
}
}
{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d2"),
"countryCode" : "DE",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
9.020215100000001,
50.8218537
]
}
}
现在创建一个 2dsphere 索引:
db.offers.createIndex({ "geoLocation": "2dsphere" })
现在您可以查询了:
db.offers.aggregate([
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [6.0950994999999999, 49.8914114000000026]
},
"distanceField": "distance",
"spherical": true,
"maxDistance": 50000
}}
])
它返回一个“近”的文档,或者实际上是 0 距离:
{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d1"),
"countryCode" : "LU",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
6.0950995,
49.8914114
]
},
"distance" : 0
}
关于mongodb - Geonear 不工作 - mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196916/
我有一个看起来像这样的集合: { "_id" : ObjectId("5783051796a90cd8098fc3e0"), "name" : "Org 1", "locati
我有一个看起来像这样的集合: { "_id" : ObjectId("5783051796a90cd8098fc3e0"), "name" : "Org 1", "locati
我在 Java 中使用 mongodb 的地理功能,我在运行命令“geoNear”时遇到以下问题: 给定: 名为“GEOENTITIES”的集合,包含 GeoJSON 对象 此集合使用 Java 代码
我有一个名为 test 的集合,我想使用 geoNear 进行查询。 2d 索引设置在字段 loc 上。 我在这里正确使用 db.runCommand 吗? db.open (err, client)
我正在使用 MongoDB 2.6.3 查询大量地理空间数据。具体来说,我正在查询中心位置几公里范围内所有 ping 的数据集,然后按用户标识符折叠它们以计算每个用户有多少 ping。 当然,我为此使
我想根据它们与 2D 空间中多个点的接近程度对我的结果进行排序。 我假设这将通过查询第一点然后根据第二点重新查询/检查这些结果来完成? 也许下面的代码可以更好地解释我想要实现的目标? db.runCo
我有以下数据: { "_id" : ObjectId("55a8c1ba3996c909184d7a22"), "uid" : "1db82e8a-2038-4818-b805-76a
在mongodb中使用geoNear时是否可以添加更多的过滤器?例如,假设我的记录如下所示: { _id: {}, cid: 1, latlon: [ -74.07096147537231, 4
我在记录集合 Mongodb (3.4) 中有文档,如下所示:- { "_id" : ObjectId("592d0c78555a7436b0883960"), "us
我有这个查询: db.places.aggregate([ { "$geoNear" : { "near" : { "type" : "Po
我正在使用 mongoDB 在 Java 应用程序中存储和获取位置相关信息。 我在使用“geoNear”获取数据时遇到异常... 这是存储数据的代码: Document doc = new Docum
我正在 MongoDB 中对我的用户模型执行查询,并希望首先按搜索结果的距离(以公里为单位)(使用 $geoNear 运算符),然后按上次登录时间对搜索结果进行排序。问题是,距离属性以浮点变量中的米形
我的收藏有如下字段,存储用户签到(地理坐标) { "_id" : ObjectId("5333c3063b15ea390b3c986a"), "userID" : "5332cad33
我正在使用 NodeJs 和 Mongoose 并构建一个功能来列出附近的交易。 Deal.db.db.command({ "geoNear": Deal.collection.name
我一直在网上搜索与此相关的内容,但找不到。 我有这个聚合 Place.aggregate( [ { "$geoNear": { "near": {
我在集合 offers 中有以下示例 mongodb 文档。 集合:优惠 { "countryCode" : "LU", "geoLocation" : [ {
我有一个简单的 MongoDo 聚合查询: { "$geoNear": { "near": { "type": "Point",
我在 MongoDB 中有一些地理空间数据,我想找到靠近用户位置并符合特定条件的地点列表。 这是我的文档结构: { "_id" : { "$oid" : "528af043d1a2760efcd2fd
我的文档结构如下: { agency_key : '', route_id: '', direction_id: '', stops: [ {stop_id:15,
我正在开发一个 PHP 应用程序,我们需要在其中检索特定边界内的结果,但按结果的创建日期而不是距离排序。我认为 MongoDB 的 geoNear 命令对此非常有用,因为它负责计算每个结果的距离。但是
我是一名优秀的程序员,十分优秀!