简体   繁体   中英

MongoDB GeoJSON weird query results

I am using GeoJSON to store coordinates of locations that i want later on to query by proximity,

my schema looks like the following:

'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);

I am using mongoose and my query looks something like the following:

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);
});

i added a new branch to the database with the following coordinates Latitude:34.237918 Longitude:36.002197 and i query the database with the following coordinates: Latitude:33.882957 Longitude:35.502319 and a maxDistance of 100

the difference between these 2 coordinates is more than 100m however the database return results, what am i missing???

Can you double check that you're doing the query that you think you are doing? Your query works fine for me:

> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM