[英]Geo Location : unable to find index for $geoNear query in nodejs
I want to find near by geo locations using LatLong, here is my code : 我想使用LatLong在地理位置附近找到,这是我的代码:
var locationSchema = new Schema({
name : {
required : "Name is required",
type : String
},
address : {
required : "address is required",
type : [Number],
index : '2d'
},
createdAt : {
type : Date,
default : new Date()
}
});
module.exports = mongoose.model('Location', locationSchema);
Now I have data in collection and I want to fetch all documents withing range of 3 KMs of provided Latlong. 现在,我已经收集了数据,我想获取所有提供的Latlong范围为3 KM的文档。
Here is my controller code for getting data : 这是我用于获取数据的控制器代码:
myLocation : function (req, res) {
var coords = [],
maxDistance = 3;
coords[0] = parseFloat(req.query.longitude);
coords[1] = parseFloat(req.query.latitude);
maxDistance /= 6371; // divide by this number as earth's approximate radius is 6371 KMs.
Location.find({
location : {
$near : coords,
$maxDistance : maxDistance
}
}, function (err, locations) {
if (err) {
res.send(err);
} else {
console.log("Location list");
res.send(locations);
}
});
{ MongoError: error processing query: ns=geolocation.locationTree: GEONEAR field=location maxdist=0.000470884 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query
at Function.MongoError.create (/var/www/NodeJS/GeoLocation/node_modules/mongodb-core/lib/error.js:31:11)
at queryCallback (/var/www/NodeJS/GeoLocation/node_modules/mongodb-core/lib/cursor.js:212:36)
at /var/www/NodeJS/GeoLocation/node_modules/mongodb-core/lib/connection/pool.js:469:18
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
name: 'MongoError',
message: 'error processing query: ns=geolocation.locationTree: GEONEAR field=location maxdist=0.000470884 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
ok: 0,
errmsg: 'error processing query: ns=geolocation.locationTree: GEONEAR field=location maxdist=0.000470884 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 2,
codeName: 'BadValue' }
Location.find({
address: {
$near: {
$geometry : {
type: 'Point',
coordinates: coords
},
$maxDistance : maxDistance * 1000
}
}
}, function (err, locations) {
console.log(locations);
});
unable to find index for $geoNear query 找不到$ geoNear查询的索引
db.collectionName.createIndex({ "location": "2d" })
refer: Create a 2d Index 参考: 创建二维索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.