首页 文章

MongoDB GeoJSON奇怪的查询结果

提问于
浏览
0

我正在使用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 回答

  • 0

    你能仔细检查一下你正在做你认为自己在做的查询吗?您的查询对我来说很好:

    > db.test.drop()
    > var p = { "type" : "Point", "coordinates" : [36.002197, 34.237918] }
    > var q = { "type" : "Point", "coordinates" : [35.502319, 33.882957] }
    > db.test.insert({ "loc" : p })
    > db.test.ensureIndex({ "loc" : "2dsphere" })
    > db.test.count({
        "loc" : {
            "$near" : {
                "$geometry" : q,
                "$maxDistance" : 100
            }
        }
    })
    0
    

相关问题