我正在尝试在mongodb中使用一些地理定位功能.使用$ near的查询查询似乎不起作用!
我目前在我的数据库中有这个对象:
{
"Username": "Deano",
"_id": {
"$oid": "533f0b722ad3a8d39b6213c3"
},
"location": {
"type": "Point",
"coordinates": [
51.50998,
-0.1337
]
}
}
Run Code Online (Sandbox Code Playgroud)
我也设置了以下索引:
{
"v": 1,
"key": {
"location": "2dsphere"
},
"ns": "heroku_app23672911.catchmerequests",
"name": "location_2dsphere",
"background": true
}
Run Code Online (Sandbox Code Playgroud)
当我运行此查询时:
db.collectionname.find({ "location" : { $near : [50.0 , -0.1330] , $maxDistance : 10000 }})
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error: {
"$err" : "can't parse query (2dsphere): { $near: [ 50.0, -0.133 ], $maxDistance: 10000.0 }",
"code" : 16535
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我哪里出错了?任何帮助将非常感激!
我使用 $geoNear 作为聚合框架的第一步。我需要根据“标签”字段过滤掉结果,它工作正常,但我看到有两种方法可以给出不同的结果。
示例 MongoDB 文档
{
“位置”: [
40.80143,
-73.96095
],
“标签”:“披萨”
}
我已将 2dsphere 索引添加到“位置”键
db.restaurants.createIndex( { '位置' : "2dsphere" } )
查询 1
db.restaurants.aggregate(
[
{
“$geoNear”:{
“附近”:{类型:“点”,坐标:[55.8284,-4.207]},
“限制”:100,
“最大距离”:10*1000,
"distanceField": "dist.calculated",
"includeLocs": "dist.location",
“距离乘数”:1/1000,
“球形”:真实
}
},{
"$match":{"tag":"pizza"}
},
{
"$group":{"_id":null,"totalDocs":{"$sum":1}}
}
]
);
查询 2
db.restaurants.aggregate(
[
{
“$geoNear”:{
“查询”:{“标签”:“披萨”}
“附近”:{类型:“点”,坐标:[55.8284,-4.207]},
“限制”:100,
“最大距离”:10*1000,
"distanceField": "dist.calculated",
"includeLocs": "dist.location",
“距离乘数”:1/1000,
“球形”:真实
}
},
{
"$group":{"_id":null,"totalDocs":{"$sum":1}}
}
]
);
分组选项只是获取两个查询返回的文档数。
两个查询返回的 totalDocs …
我目前正在尝试构建一个2dsphere索引,但创建似乎失败了.
索引创建失败的文档是有效的geojson(根据geojsonlint).
另据我所知,它遵循MongoDB"Polygon"规则.
我很感激任何帮助,因为我无法弄清楚为什么索引创建似乎失败了.
提前致谢!
db.poly.ensureIndex( { loc: "2dsphere" } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"ok" : 0,
"errmsg" : "Can't extract geo keys from object, malformed geometry?: { _
id: 353, loc: { type: \"Polygon\", coordinates: [ [ [ 8.090732000000001, 53.6379
766 ], [ 8.050639500000001, 53.6250853 ], [ 8.036974600000001, 53.6286108 ], [ 7
.994035500000001, 53.6016978 ], [ 8.0120927, 53.59855020000001 ], [ 8.0102720000
00001, 53.5883803 ], [ 8.023379, 53.5867745 ], [ 8.0148004, 53.5832729 ], [ 8.02
6839500000001, …Run Code Online (Sandbox Code Playgroud) 我索引了2dsphere上的字段loc,我无法在Point类型的GeoJson数据上运行geowithin查询.
这是查询:
db.test.find( { loc :
{ $geoWithin :
{ $geometry :
{ type : "Polygon" ,
coordinates : [ [ [-74.6862705412253, 40.42341005] ,
[-75.0846179, 39.9009465 ],
[-74.20570119999999, 41.0167639 ]
]
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
uncaught exception: error: {
"$err" : "Can't canonicalize query: BadValue bad geo query",
"code" : 17287
}
Run Code Online (Sandbox Code Playgroud)
文件结构:
{
"_id" : ObjectId("53d15e7132e7b7978c472e6e"),
"loc" : {
"type" : "Point",
"coordinates" : [ -74.6862705412253, 40.42341005 ]
},
}
Run Code Online (Sandbox Code Playgroud)
索引:
{
"0" : {
"v" : …Run Code Online (Sandbox Code Playgroud) 当我尝试创建几何的索引db.polygons.createIndex({"geometry":"2dsphere"}),它停止在错误代码16755.它说,它有一定的多边形Can't extract geo keys和Duplicate vertices: 18 and 20。
因此,在进一步检查时,当多边形中的2个节点彼此靠近甚至重复时,似乎会发生这种情况。
然后,我手动在QGIS中删除此节点,然后重试该过程,却发现存在另一个存在相同问题的多边形。
如何解决此问题,而不必重复整个固定多边形的过程>上传到MongoDB>创建索引?有没有办法找出这个问题有多少个多边形?
Mongodb 站点显示 2d 索引的内部结构是GeoHash.
我想知道 2dsphere 索引的内部结构。
是它GeoHash,Cartesian Tiers还是其他技术?
我刚刚找到了mongodb站点的博客new-geo-features,介绍了2dsphere index。
但是没有细节。
当我尝试在 mongoDB 中创建 2dsphere 索引时,我收到以下错误消息:
\n\n> db.mahalle.createIndex({geometry:"2dsphere"})\n{\n "createdCollectionAutomatically" : false,\n "numIndexesBefore" : 1,\n "errmsg" : "exception: Can\'t extract geo keys: { _id: ObjectId(\'55b4e025a6ef7e9c50ae79cd\'), type: \\"Feature\\", p\nroperties: { ADI: \\"\xc3\x87elemli\\", ILADI: \\"Adana\\", ILCEADI: \\"Y\xc3\xbcre?ir\\", BUCAKADI: \\"Merkez\\", KOYADI: \\"\xc3\x87elemli\\", ILKOD:\n 1 }, geometry: { type: \\"Polygon\\", coordinates: [ [ [ 35.624498, 36.849395 ], [ 35.649357, 36.876937 ], [ 35.657724, 3\n6.879572 ], [ 35.663334, 36.873718 ], [ 35.668063, 36.875857 ], [ 35.671228, 36.87511 ], [ 35.676613, 36.872193 ], [ 35.\n676789, 36.86627 ], …Run Code Online (Sandbox Code Playgroud) 2dsphere ×7
mongodb ×7
geospatial ×4
geojson ×3
coordinates ×1
geolocation ×1
geometry ×1
polygon ×1