MongoDB:使用 $geoWithin 运算符没有得到正确的结果 [英] MongoDB: Not getting correct result using $geoWithin operator
问题描述
我正在使用 $geoWithin
作为圆圈,但我没有得到预期的结果.有两个集合,一个用于 users
,第二个用于 items
.我正在为项目设置半径和坐标.所以我必须在那个坐标和半径内找到一个用户.
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.
用户收藏
{
"_id" : NumberLong(25287),
"_class" : "com.samepinch.domain.user.User",
"name":"XYZ",
"location" : [
74.866247,
31.63336
]
}
物品集合
{
"_id" : NumberLong(46603),
"itemName" : "Chandigarh",
"categoryName" : "TRAVELLING",
"location" : {
"_id" : null,
"longitude" : 77.15319738236303,
"latitude" : 28.434568229025803,
"radius" : 29153.88048637637
}
}
根据物品坐标和用户坐标,两地之间的距离约为500公里.
as according to item coordinates and user coordinates,there is approx 500 km distance between two places.
查询
db.users.find({ location: { $geoWithin: { $center: [ [77.15319738236303,28.434568229025803], 29153.88048637637/3963.2] } } } ).count()
根据 $geoWithin,用户不应该显示,但它正在显示.如果我做错了什么,请帮助我.
According to $geoWithin,user should not show,but it is showing. If i am doing something wrong,please help me.
谢谢
推荐答案
我认为你的问题是,你搜索的范围太广了.根据 MongoDB 文档,$ 的正确语法centerSphere
是:
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 ] }
}
} )
您现在正在搜索点周围 29153.88048637637 英里半径内的点,并且两个点都在您定义的中心周围的半径内.
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.
希望对你有帮助:)
这篇关于MongoDB:使用 $geoWithin 运算符没有得到正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!