【问题标题】:unable to find index for $geoNear query找不到 $geoNear 查询的索引
【发布时间】:2023-04-01 03:02:02
【问题描述】:

我正在使用 node 和 mongodb 编写应用程序,但无法进行任何地理位置查询。我正在邮寄这个查询:

{  
    "geometry": {
          "type": "Point",
          "coordinates" : [-8.139530, -79.032231 ]
    }
}

这是我服务器的输出:

    { MongoError: Unable to execute query: error processing query: ns=dblocations.locations limit=1000 skip=0                                     
Tree: GEONEAR  field=geometry maxdist=0.00125569 isNearSphere=0                                                                               
Sort: {}                                                                                                                                      
Proj: {}                                                                                                                                      
 planner returned error: unable to find index for $geoNear query                                                                              
    at Function.MongoError.create (/Users/giuseppemarotta/Documents/Mockinghi/node_modules/mongodb-core/lib/error.js:31:11)                   
    at queryCallback (/Users/giuseppemarotta/Documents/Mockinghi/node_modules/mongodb-core/lib/cursor.js:198:34)                              
    at /Users/giuseppemarotta/Documents/Mockinghi/node_modules/mongodb-core/lib/connection/pool.js:436:18                                     
    at _combinedTickCallback (internal/process/next_tick.js:67:7)                                                                             
    at process._tickCallback (internal/process/next_tick.js:98:9)                                                                             
  name: 'MongoError',                                                                                                                         
  message: 'Unable to execute query: error processing query: ns=dblocations.locations limit=1000 skip=0\nTree: GEONEAR  field=geometry maxdist
=0.00125569 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',                            
  '$err': 'Unable to execute query: error processing query: ns=dblocations.locations limit=1000 skip=0\nTree: GEONEAR  field=geometry maxdist=
0.00125569 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',                             
  code: 17007 } 

我尝试使用本地 mongoshell/mongoose 查询,但无法正常工作。

这是我的位置架构:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;


var LocationSchema = new mongoose.Schema({

  name: String,
  geometry: {
        coordinates: { type: [Number], required:true }
    },
  created_by : { type: String, required: true},
    category: String,
    description: String,
    rating: Number,
  created_at: Date,
  updated_at: Date
});

// on every save, add the date
LocationSchema.pre('save', function(next) {
  // get the current date
  var currentDate = new Date();
  rating = 0 ;
  // change the updated_at field to current date
  this.updated_at = currentDate;

  // if created_at doesn't exist, add to that field
  if (!this.created_at)
    this.created_at = currentDate;

  next();
});

LocationSchema.index({ geometry: '2dsphere' });

module.exports=mongoose.model('Location', LocationSchema);

这是我的 server.js:

       //lets require/import the mongodb native drivers.
var mongoose = require('mongoose');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.


// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://--------:-------@ds021895.mlab.com:21895/dblocations';
var Location = require('./models/location');
var User = require('./models/user');

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

mongoose.connect(url);

// server.js

// BASE SETUP
// =============================================================================

// call the packages we need
var express    = require('express');        // call express
var app        = express();                 // define our app using express
var bodyParser = require('body-parser');


router.route('/query')
// create a location (accessed at POST http://localhost:8080/mockinghi/register)
    .post(function(req, res) {

    var limit = req.query.limit || 10;
    limit=10;
    // get the max distance or set it to 8 kilometers
    var maxDistance = req.query.distance || 8;
    var minDistance = 0;
    // we need to convert the distance to radians
    // the raduis of Earth is approximately 6371 kilometers
    maxDistance /= 6371;

    // get coordinates [ <longitude> , <latitude> ]
    var coords = [];
    console.log(req);
    coords = req.body.geometry.coordinates;

    console.log("coords = "+coords);    
    console.log("MAxDistance = "+maxDistance);
/*
    // Use connect method to connect to the Server
    MongoClient.connect(url, function (err, db) {
    if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
    } else {
        //HURRAY!! We are connected. :)
        console.log('Connection established to', url);
        var locations_col = db.collection('locations');
        // do some work here with the database.

        locations_col.createIndex({ "geometry": "2d" });

        locations_col.createIndex({ "geometry.coordinates": "2d" });
        locations_col.ensureIndex({ "geometry" : "2dsphere" });
        locations_col.ensureIndex({ "geometry.coordinates" : "2dsphere" });


        locations_col.find(
            {
                geometry: {         
                $near :
                    {
                        $geometry: { type: "Point", 
                                    coordinates:[ coords[0],
                                                coords[1] ] },
                        $maxDistance: maxDistance
                    },
                }
            }
        ).toArray(function (err, result) {
                if (err) {
                    console.log(err);
                } else if (result.length) {
                    console.log('Found:', result);
                } else {
                    console.log('No document(s) found with defined "find" criteria!');
                }
            }
        );



        //Close connection
        db.close();

    }
    }); 


*/

    // find a location
    Location.find({
      loc: {        
        $near :
              {
                $geometry: { type: "2d", 
                             coordinates:[ coords[0],
                                           coords[1] ] },
                $maxDistance: maxDistance
              },
      }
    }).limit(limit).exec(function(err, locations) {
      if (err) {
        return res.json(500, err);
      }
    //console.log(locations);
    res.json(200, locations);
    });


    });

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

您需要为您的条目创建索引,因为错误明确指出$geoNear 没有索引。

db.collection_name.createIndex({"geometry.coordinates": "2dsphere"}))

【讨论】:

    【解决方案2】:

    需要为geojson对象,类型为字符串,但默认为点。还有其他一些东西,比如多边形、线等,但你只是存储点。记住坐标是[经度,纬度]

    geometry: {
        type: {
            type: String,
            default: "Point"
        },
        coordinates: {
            type: [Number]
        },
        index: '2dsphere'
    }
    

    你也可以把索引放在别的地方

    LocationSchema.index({geometry: '2dsphere'});
    

    【讨论】:

      【解决方案3】:

      将您的模型更改为:

      var mongoose     = require('mongoose');
      var Schema       = mongoose.Schema;
      
      
      var LocationSchema = new mongoose.Schema({
      
        name: String,
        geometry: {
              coordinates: { type: [Number], required:true }
          },
        created_by : { type: String, required: true},
          category: String,
          description: String,
          rating: Number,
        created_at: Date,
        updated_at: Date
      });
      
      // on every save, add the date
      LocationSchema.pre('save', function(next) {
        // get the current date
        var currentDate = new Date();
        rating = 0 ;
        // change the updated_at field to current date
        this.updated_at = currentDate;
      
        // if created_at doesn't exist, add to that field
        if (!this.created_at)
          this.created_at = currentDate;
      
        next();
      });
      
      LocationSchema.index({ geometry: '2dsphere' });
      
      module.exports=mongoose.model('Location', LocationSchema);
      

      【讨论】:

      • Same : "$err": "无法执行查询:错误处理查询:ns=dblocations.locations limit=10 skip=0\nTree: GEONEAR field=loc maxdist=0.00125569 isNearSphere=0\ nSort: {}\nProj: {}\n 规划器返回错误:无法找到 $geoNear 查询的索引”,
      • 尝试类似:"geometry" : { "$near" : { "$geometry": { "type" : "Point", "coordinates": [parseFloat(MyLongitude), parseFloat(MyLatitude )] }, "$maxDistance" : parseFloat(MyRadius) }
      猜你喜欢
      • 1970-01-01
      • 2014-06-05
      • 2020-01-30
      • 1970-01-01
      • 2018-10-28
      • 2015-01-06
      • 1970-01-01
      • 2017-03-24
      • 2021-10-28
      相关资源
      最近更新 更多