【发布时间】:2013-05-24 23:06:53
【问题描述】:
我正在尝试从我的地点集合中获取一些接近坐标的结果。为此,我在 location.position 属性上使用了二维索引,该属性具有 lng 和 lat 属性。
问题是我放置的每个坐标,它返回相同的 60 个结果(整个集合)。
我收藏中的文档都来自我的 GPS 坐标附近,但是,如果我放置一个 0.0、0.0 坐标或任何其他对,它总是返回相同的。
事实上,如果我使用空的 find(),查询返回的结果完全相同。
索引似乎已正确创建,因为它没有返回任何错误。
我的收藏中的一个对象是这样的:
{ "_id" : "80293840923...",
"name": "myname",
"location" : {
"position" : { "lng": -196988, "lat": 43.30594 }
"address": "example",
"city": "example" }
}
索引是这样创建的:
places_collection.ensureIndex({"location.position": "2d"}, {name: "index2dLocation"}, function(error, indexName){
callback("", "Indice creado: " + indexName)
});
搜索会是这样的:
places_collection.find(
{ "location.position" : { $near : [ parseFloat(-123.98) , parseFloat(162.342) ] } }
).toArray(function(error, results) {
if( error ){ callback(error, "")
}else{ console.log(results.length); callback(null, results); }
});
最后,如前所述,我的搜索总是返回相同的结果。不管我放一个坐标还是另一个坐标。即使我创建一个空的 find() 也不在乎。
这里有什么帮助吗?我不知道如何让它工作。
非常感谢。
【问题讨论】: