简体   繁体   English

地理位置:无法在nodejs中为$ geoNear查询找到索引

[英]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.

相关问题 无法找到$ geoNear查询的索引 - unable to find index for $geoNear query 在查询附近过滤地理位置的环回[错误:找不到$ geoNear查询的索引] - Loopback where filter geo near query [error: unable to find index for $geoNear query] 使用猫鼬无法找到 $geoNear 查询错误的索引 - Unable to find index for $geoNear query error with mongoose 即使创建了索引,也无法找到$ geoNear查询的索引 - Unable to find index for $geoNear query even when index was created Mongo(+ Mongoose)错误:无法找到$ geoNear查询的索引 - Mongo (+Mongoose) error: Unable to find index for $geoNear query 找不到 $geoNear 查询的索引,代码:2,代号:'badValue' - Unable to find index for $geoNear query, code: 2, code-name: 'badValue' 得到错误“ mongodb无法为$ geoNear查询找到索引” - Getting error “mongodb unable to find index for $geoNear query” $ near error - planner返回错误:无法找到$ geoNear查询的索引 - $near error - planner returned error: unable to find index for $geoNear query 在 MongoDB 中创建索引但每次都会出现错误:无法找到 $geoNear 查询的索引 - Created Index in MongoDB but the error comes everytime: unable to find index for $geoNear query 是否可以使用 NodeJS 根据 ip 地址查找设备地理位置? - Is it possible to find device geo location based on ip address using NodeJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM