我编写了一个应用程序,它查找本地机构并通过RESTful API交付它们。我的堆栈是:快递,快递-资源和猫鼬。下面是我的模型的一个示例:
var PlaceSchema = new mongoose.Schema(
{
name: {
type: String,
required: true
},
geolocation: {
lat: Number,
lng: Number
},
address: {
type: String
}
}
);
PlaceSchema.index({
geolocation: '2d'
});我已经检查过几次了,我的lon/lat值被正确地存储在db中,所以没有任何数据错误。
然后运行查询以获取具有特定查询值的数据:
mongoose.connection.db.executeDbCommand(
{
geoNear: 'places',
near: [
parseFloat(req.body.lat),
parseFloat(req.body.lng)
],
num: limit,
query: {
// Some additional queries that
// don't impact the geo results.
},
spherical: true,
distanceMultiplier: 6371, // converting results to km
maxDistance: distance / 6371,
},
function(err, result)
{
res.send(200, {
status: true,
data: theaters
});
}
);它返回的结果有几个问题:( a)以km表示的计算是错误的。在大多数情况下,有6-7公里的差异,但它是不同的,b)更近的地方出现比其他地方更远。
我使用直接的猫鼬查询,因为它将返回计算出的距离( API返回所需的距离)。切换到猫鼬查找方法显然不会让我访问这些数据。
想知道我的指数是不是错了?是不是应该是2dsphere呢?这方面的文档有点混乱,但我看到的大多数示例只使用2d。
谢谢一堆人!
发布于 2013-05-19 21:19:04
无论您在MongoDB中使用哪种类型的地理空间索引,始终必须先存储经度,然后存储纬度。
来自http://docs.mongodb.org/manual/core/2d/#store-points-on-a-2d-plane和文档中的多个其他地方:
无论是作为数组还是文档,如果使用经度和纬度,按以下顺序存储坐标:经度、纬度.
https://stackoverflow.com/questions/16635955
复制相似问题