问题描述
我正在尝试使用mongodb中的某些地理位置功能.在$ near中使用查找查询似乎无效!
I'm trying to make use of some geolocation functionality in mongodb. Using a find query with $near doesn't seem to work!
我当前在数据库中有该对象:
I currently have this object in my database:
{
"Username": "Deano",
"_id": {
"$oid": "533f0b722ad3a8d39b6213c3"
},
"location": {
"type": "Point",
"coordinates": [
51.50998,
-0.1337
]
}
}
我还设置了以下索引:
{
"v": 1,
"key": {
"location": "2dsphere"
},
"ns": "heroku_app23672911.catchmerequests",
"name": "location_2dsphere",
"background": true
}
当我运行此查询时:
db.collectionname.find({ "location" : { $near : [50.0 , -0.1330] , $maxDistance : 10000 }})
我收到此错误:
error: {
"$err" : "can't parse query (2dsphere): { $near: [ 50.0, -0.133 ], $maxDistance: 10000.0 }",
"code" : 16535
}
有人知道我要去哪里了吗?任何帮助将不胜感激!
Does anyone know where I'm going wrong? Any help would be much appreciated!
推荐答案
如果您的数据也采用GeoJSON格式,则似乎需要使用GeoJSON格式.如果您使用:
It seems you need to use the GeoJSON format if your data is in GeoJSON format too, as yours is. If you use:
db.collectionname.find({
"location": {
$near: {
$geometry:
{ type: "Point", coordinates: [50.0, -0.1330] }, $maxDistance: 500
}
}
})
它应该工作.我可以使用该字段的GeoJSON存储格式来复制您的错误,但是该文档在查询表达式中称为旧点.我认为文档尚不清楚,因为它们建议您可以将GeoJSON和旧式坐标与2dsphere索引一起使用 2dsphere
it should work. I could replicate your error using GeoJSON storage format for the field, but what the docs call legacy points in the query expression. I think the docs are a bit unclear in that they suggest you can use both GeoJSON and legacy coordinates with a 2dsphere index 2dsphere
我正在使用2.4.10,因为它的价值是什么,因为2.4版本中的空间有一些大变化.
I am using 2.4.10, for what it is worth, as there were some big changes to spatial in the 2.4 release.
这篇关于MongoDB使用$ near查找查询并且坐标不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!