Node.js 在MongoDB中创建了索引,但每次都会出现错误:无法找到$geoNear查询的索引

Node.js 在MongoDB中创建了索引,但每次都会出现错误:无法找到$geoNear查询的索引,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想从mongodb中的nodejs文件中获取数据(附近的位置)。我在mongodb中创建了一个索引,但是当我查询它时,我得到了以下错误: { name: 'MongoError', message: 'Unable to execute query: error processing query: ns=event_management.locations limit=1000 skip=0\nTree: GEONEAR field=loc maxdist=500000 isNearSphe

我想从mongodb中的nodejs文件中获取数据(附近的位置)。我在mongodb中创建了一个索引,但是当我查询它时,我得到了以下错误:

{ name: 'MongoError', 
message: 'Unable to execute query: error processing query:
ns=event_management.locations limit=1000 skip=0\nTree: GEONEAR
field=loc maxdist=500000 isNearSphere=0\nSort: {}\nProj: {}\n planned
returned error: unable to find index for $geoNear query',
'$err':Unable to execute query: error processing
query:ns=event_management.locations limit=1000 skip=0\nTree: GEONEAR  
field=loc maxdist=500000 isNearSphere=0\nSort: {}\nProj: {}\n planner 
returned error: unable to find index for $geoNear query',
code: 17007 }
我用于从节点获取数据的查询是:

domain.Location.find(
{
  loc:{
    $near :{$geometry:{type:"Point",coordinates: [parseFloat(longitude), parseFloat(latitude) ] },
    $maxDistance: parseFloat(distance)*1000}
  }
}
用于创建索引的命令是:

db.locations.ensureIndex({ loc: "2dsphere"});
我不知道我错在哪里。如果有人可以的话,请帮我


提前谢谢。

根据我的说法,有一个与距离($maxDistance)相关的错误。你超出了限制。因此,无法找到具有该条件的索引。我只是对它有点信心

domain.Location.find(
{
  loc:{
    $near :{$geometry:{type:"Point",coordinates: [parseFloat(longitude), parseFloat(latitude) ] },
    $maxDistance: parseFloat(distance)}
  }
}

据我所知,有一个与距离($maxDistance)有关的错误。你超出了限制。因此,无法找到具有该条件的索引。我只是对它有点信心

domain.Location.find(
{
  loc:{
    $near :{$geometry:{type:"Point",coordinates: [parseFloat(longitude), parseFloat(latitude) ] },
    $maxDistance: parseFloat(distance)}
  }
}

如何在数据库中存储lat和lng??loc:{type:{type:String,必需:true,枚举:['Point',LineString',Polygon'],默认值:'Point'},坐标:[]}lat和long以字符串格式存储。上面的代码是在nodeHow中定义的模型,您如何在DB中存储lat和lng??loc:{type:{type:String,required:true,enum:['Point',LineString',Polygon'],默认值:'Point'},坐标:[]}lat和long以字符串格式存储。以上代码是节点中定义的模型