本文介绍了MongoDB:使用$ geoWithin操作符没有得到正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为使用 $ geoWithin
,并且我没有得到预期的结果。有两个集合,一个用于用户
,另一个用于项目
。我正在设置项目的半径和坐标。所以我必须在该坐标和半径内找到用户。
用户集合
{
_id:NumberLong(25287),
_class:com.samepinch.domain.user.User,
name:XYZ,
location:[
74.866247,
31.63336
]
}
项目集合
{
_id:NumberLong(46603),
itemName:Chandigarh,
categoryName:TRAVELING,
location:{
_id:null,
longitude: 77.15319738236303,
latitude:28.434568229025803,
radius:29153.88048637637
}
}
根据物品坐标和用户坐标,两个地点之间的距离大约为500公里。
查询
db.users.find({location:{$ geoWithin:{$ center:[[77.15319738236303,28.43456822902580 3],29153.88048637637 / 3963.2]}}}).count()
根据$ geoWithin,不应该显示,但它显示。如果我做错了什么,请帮助我。
谢谢 解决方案
我认为你的问题在于,你在半径太大的范围内搜索。
根据,正确的语法 $ centerSphere
是:
db。< name> .find( {
loc:{$ geoWithin:
{
$ centerSphere:[[< longitude>,< latitude>],
< MILES> /3963.2]中的半径}}
})
您现在正在29153.88048637637英里半径内搜索点围绕你的观点,并且这两个点都在你定义的中心附近的半径内。
我希望这可以帮助你:)
I am using $geoWithin
for circle, and i am not getting expected result. There are two collections,one for users
and second for items
. I am setting radius and coordinates for item. so i have to find a users within that coordinates and radius.
User collection
{
"_id" : NumberLong(25287),
"_class" : "com.samepinch.domain.user.User",
"name":"XYZ",
"location" : [
74.866247,
31.63336
]
}
Item collection
{
"_id" : NumberLong(46603),
"itemName" : "Chandigarh",
"categoryName" : "TRAVELLING",
"location" : {
"_id" : null,
"longitude" : 77.15319738236303,
"latitude" : 28.434568229025803,
"radius" : 29153.88048637637
}
}
as according to item coordinates and user coordinates,there is approx 500 km distance between two places.
Query
db.users.find({ location: { $geoWithin: { $center: [ [77.15319738236303,28.434568229025803], 29153.88048637637/3963.2] } } } ).count()
According to $geoWithin,user should not show,but it is showing. If i am doing something wrong,please help me.
Thanks
解决方案
I think your problem is, you are searching in a far too wide radius.According to the MongoDB documentation, the correct syntax of $centerSphere
is:
db.<name>.find( {
loc: { $geoWithin:
{
$centerSphere: [ [ <longitude>, <latitude> ],
<radius in MILES>/3963.2 ] }
}
} )
You are now searching points in a 29153.88048637637 mile radius around your point, and both points are in that radius around the center you defined.
I hope this helps you :)
这篇关于MongoDB:使用$ geoWithin操作符没有得到正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!