【问题标题】:MongoError: can't get query executor with geoNear MongooseMongoError:无法使用 geoNear Mongoose 获取查询执行器
【发布时间】:2017-04-21 03:45:31
【问题描述】:

我正在尝试在 Moongose 中使用 geoNear 从 DB 获取结果,但它给出了“无法获取查询执行程序”错误。

 
// Schema
let dealSchema = new Schema({
userId: {type: String, required: true},
images: {type: [String], required: true},
location: {
    lng: {type: Number},
    lat: {type: Number},
    name: {type: String, required: true}
},
description: {type: String, required: false, default: ""},
expiredReports: {type: [String], required: false},
inappropriateReports: {type: [String], required: false},
likes: {type: [String], required: false},
dislikes: {type: [String], required: false},
comments: {type: [commentSchema], required: false},
creationDate: {type: Date, default: new Date()},
updationDate: {type: Date, default: new Date()}
}, {collection: TableName.DEAL});


function getAll(radius) {
var point = {type: "Point", coordinates: [9, 9]};

        let distance = parseInt(radius);
        if (isNaN(distance)) {
            return callback(new Error("Invalid Radius"), null)
        }

        var geoOptions = {
            spherical: true,
            maxDistance: distance,
            num: 10
        };

        DealData.geoNear(point, geoOptions, function (err, docs, stats) {
                console.log(docs);
            }
        );
   
}

这是我的 Mongoose 模型和我从数据库中获取数据的代码。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    geoNear 方法需要 2dsphere 索引,创建该索引后,您将能够通过坐标查询数据。

    例子:

    var mySchema = new mongoose.Schema({
        name: {type: String, required: true},
        location: {type: [Number], index: '2dsphere'}
      });
    

    位置字段的第一项是经度,第二项是纬度

    【讨论】:

    • 非常感谢@Artem
    猜你喜欢
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 2019-06-17
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多