javascript - 使用 mongoDB 和 mongoose 获取附近的地点

标签 javascript node.js mongodb mongoose nosql

我有一个小型数据库,其中包含我手动添加的位置,我正在尝试按 minDistance 和 maxDistance 过滤它们。

这就是 Mongoose 模式

 var schema = new Schema({
    name: {
        type: String,
        unique: false,
        required: true
    },
    location: {
        type: [Number],  // [<longitude>, <latitude>]
        index: '2dsphere',     // create the geospatial index
        required: false // true senare
    }
});

这是用于获取附近地点的方法:

schema.statics.getNearby = function (longitude, latitude, minDistance, maxDistance, callback) {

    if ((longitude || latitude) === undefined) return new ModelError("location or radius is missing");

    var Places = this;
    var point = { type: "Point", coordinates: [ longitude, latitude]};
    Places.geoNear(point, { minDistance: minDistance, maxDistance : maxDistance},
        function(err, activities, stats) {
            if (err)  return callback(err);
            callback(null, activities);
    });
};

这是我执行此方法时得到的错误代码:

  500 Error
   at Error.MongoError (/home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
   at Function.MongoError.create (/home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
   at /home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:768:66
   at Callbacks.emit (/home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:84:3)
   at null.messageHandler (/home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:218:23)
   at Socket.<anonymous> (/home/ec2-user/bored_server/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22)
   at Socket.emit (events.js:95:17)
   at Socket.<anonymous> (_stream_readable.js:765:14)
   at Socket.emit (events.js:92:17)
   at emitReadable_ (_stream_readable.js:427:10)

让我烦恼的是,使用 mongodb,我可以执行这样的查询,而且效果很好。

db.places.find({ 
    location: { 
        $nearSphere : { 
            $geometry: { 
                type: "Point",  
                coordinates: [ 18, 58] }, 
                $minDistance: 0, 
                $maxDistance: 1000000 
                } 
            } 
        }
    )

编辑1: 他的解决方案是他应该以米为单位指定 maxDistance 值,我看不出这对我有什么帮助,因为无论我指定什么值作为 maxdistance,我都会收到错误。谢谢

最佳答案

我找到了解决方案,minDistance和maxDistance是字符串类型,对于maxDistance工作正常,但是对于minDistance我必须将其转换为float或int。 一个肮脏的解决方案是这样的代码:

schema.statics.getNearby = function (longitude, latitude, minDistance, maxDistance, callback) {

if ((longitude || latitude) === undefined) return new ModelError("location or radius is missing");

var Places = this;
var point = { type: "Point", coordinates: [ longitude, latitude]};
Places.geoNear(point, { minDistance: parseFloat(minDistance), maxDistance : maxDistance},
    function(err, activities, stats) {
        if (err)  return callback(err);
        callback(null, activities);
});

};

现在只有当 minDistance 是字符串类型时才需要这样做。 谢谢。

关于javascript - 使用 mongoDB 和 mongoose 获取附近的地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023927/

相关文章:

node.js - Puppeteer - 单击 div 不起作用

Mongodb选择片键

javascript - Meteor - 服务器端 API 调用并每分钟插入 mongodb

node.js - 使用 Nodejs 和 Imagemagick 调整图像大小

javascript - r2d3:d3.js 条形图在调整大小时消失

java - 调用匿名 JavaScript 函数

javascript - 如何使 selectize.js 选择框需要一个值?

javascript - 返回似乎没有等待 javascript

node.js - 有没有办法使用 cron 重启 pm2 进程,但前提是它尚未运行?

javascript - 使用 D3.js 的 typescript 类型转换