我正在使用GeoJSON存储我想要稍后通过邻近查询的位置坐标,
我的架构如下所示:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var BranchSchema = new Schema({
parentId: {
type: Schema.Types.ObjectId,
required: 'The ID of the restaurant is required.',
index: true
},
name: {
type: 'String',
required: 'The name is required.'
},
loc: {
'type': {
type: 'String',
default: 'Point'
},
coordinates: {
type: [Number],
default: [0,0]
}
}
});
BranchSchema.index({loc: "2dsphere"});
module.exports = mongoose.model('Branch', BranchSchema);
我正在使用mongoose,我的查询类似于以下内容:
Branch.where('loc').near({
center: [long, lat],
maxDistance: proximity,
spherical: true
}).exec(function (err, branches) {
if (err) {
return res.status(400)
.send({
message: errors.getErrorMessage(err)
});
}
return res.json(branches);
});
我使用以下坐标向数据库添加了一个新分支:纬度:34.237918经度:36.002197我使用以下坐标查询数据库:纬度:33.882957经度:35.502319,最大距离为100
这两个坐标之间的差异超过100米,但数据库返回结果,我缺少什么?
1 回答
你能仔细检查一下你正在做你认为自己在做的查询吗?您的查询对我来说很好: