我正在尝试使用mongodb $ near运算符根据用户的位置从数据库中获取一些文档。
因此,在我获得用户坐标后,我会查询一个具有正确地理空间索引的集合:
computed_places.ensureIndex({'geoJson.coordinates' : "2d"}, function(err,ok) {)
使用此查询:
computed_places.find({'geoJson.coordinates' :
{$near:
{$geometry:
{type : "Point" ,
coordinates:[user.longitude , user.latitude]}},
$maxDistance : 200
}})
})
但是在第一次测试之后,它听起来像是我输入的随机文件
longitude: 11.870853799999999,
latitude: 45.4109512
返回的文件有这个值(每个文件一对,这是' geoJson.coordinates'字段的内容):
[7.42149, 9.00046 ]
[ -1.50372, 12.3791 ]
[ 15.30972, -4.41944 ]
[ 15.0444, 12.1047 ]
[ -16.27479, 12.58644 ]
[ -16.47951, 14.69669 ]
[ -16.96598, 14.79575 ]
[ -17.46682, 14.68189 ]
[ -17.45258, 14.69922 ]
[ -17.46331, 14.68731 ]
[ -17.46232, 14.69187 ]
[ -16.42596, 16.06252 ]
[ 29.74278, -2.61611 ]
[ 30.06219, -1.95994 ]
[ 30.21835, 0.67669 ]
[ 30.27613, 0.65909 ]
[ 30.65717, -0.61551 ]
[ -9.54612, 30.40618 ]
[ 32.01435, 0.0026 ]
[ 32.01889, 1.8425 ]
[ 32.31678, 2.78819 ]
[ 32.56815, 0.33572 ]
[ 32.57702, 0.33791 ]
[ 32.58911, 0.41068 ]
[ 32.59748, 0.32096 ]
[ 32.62691, 0.34929 ]
[ 32.6354, 0.26048 ]
[ -8.02018, 31.64532 ]
[ -8.01542, 31.64902 ]
[ -4.97333, 34.06444 ]
[ -6.86187, 33.98307 ]
[ -6.8614, 33.98705 ]
[ -1.89851, 34.65022 ]
[ -6.80016, 34.04182 ]
[ 35.08467, 0.25603 ]
[ 31.053, -17.784 ]
[ 33.99323, -11.422 ]
[ 32.54222, 15.61222 ]
[ -5.89351, 35.73639 ]
[ 33.52946, 14.38584 ]
[ 36.82248, -1.27937 ]
[ -4.47724, 36.71546 ]
[ -6.29799, 36.53392 ]
[ -3.60341, 37.17572 ]
[ -3.6021, 37.17812 ]
[ -3.60446, 37.18048 ]
[ -3.59712, 37.1935 ]
[ -3.59772, 37.20488 ]
[ -5.99051, 37.3808 ]
[ -6.06589, 37.37031 ]
[ -1.17451, 38.02272 ]
[ 28.1617, -25.7322 ]
[ -3.46826, 37.99136 ]
[ -4.80046, 37.86933 ]
[ -4.78746, 37.87324 ]
[ -4.78191, 37.88005 ]
[ -4.7673, 37.8851 ]
[ -0.68716, 38.27538 ]
[ 27.99889, -26.18333 ]
[ -0.51224, 38.38446 ]
[ 18.8647, -33.9301 ]
[ 38.50135, 7.06233 ]
[ -9.19215, 38.6979 ]
[ 38.75917, 9.04667 ]
[ -9.11664, 38.7562 ]
[ -0.06884, 39.99415 ]
[ 27.72312, -29.45066 ]
[ -5.6689, 40.96603 ]
[ 2.16353, 41.38636 ]
[ 2.11051, 41.39367 ]
[ 0.62332, 41.60818 ]
[ 32.5833, -25.9667 ]
[ 32.58496, -25.974 ]
[ 32.59976, -25.95979 ]
[ -0.88738, 41.68331 ]
[ -8.39647, 41.56104 ]
[ -5.5597, 42.61231 ]
[ 29.89667, 31.19626 ]
[ 29.90528, 31.20194 ]
[ 31.20797, 30.02733 ]
[ 29.9989, 31.26448 ]
[ 25.10237, 35.31901 ]
[ 31.22038, 30.0574 ]
[ 31.23613, 30.04574 ]
[ 31.2625, 30.04583 ]
[ 5.44321, 43.23243 ]
[ 5.44209, 43.23268 ]
[ 5.35897, 43.29198 ]
[ 5.40025, 43.28711 ]
[ -5.77641, 43.24242 ]
[ 5.38261, 43.29817 ]
[ 5.37486, 43.29979 ]
[ 5.39214, 43.30123 ]
[ 5.37923, 43.30398 ]
[ -3.79904, 43.47363 ]
[ 5.39339, 43.30592 ]
[ 5.45815, 43.31909 ]
[ 5.41609, 43.33851 ]
[ 31.60639, 30.15028 ]
[ 22.37148, 37.52703 ]
与我的输入相比,这些结果似乎是随机的,但也在他们自己之间。我究竟做错了什么?这是查询文件的结构:
doc = {
"geoname_id": 6269578
, names: {
en: "Pedagogical University"
}
, country: "VN"
, geoJson: {
type: "Point"
, coordinates: [
106.68259
, 10.76154
]
}
, timezone: "Asia/Ho_Chi_Minh"
, "_id": new ObjectID("530dffbe734e07e50d9b5ef9")
};