MongoError:无法使用geoNear Mongoose获取查询执行程序

时间:2017-04-20 15:46:42

标签: node.js mongodb mongoose

我正在尝试使用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和My代码模型,用于从数据库中获取数据。

1 个答案:

答案 0 :(得分:0)

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

示例:

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

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