Node.js 即使在创建索引时,也无法找到$geoNear查询的索引
我有下面的模式,其中我有一个关于地理坐标的2d索引Node.js 即使在创建索引时,也无法找到$geoNear查询的索引,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有下面的模式,其中我有一个关于地理坐标的2d索引 var mongoose = require("mongoose"); var Schema = mongoose.Schema; var LocationSchema = new Schema ({ geo: { type: [Number], required: true, index: "2dsphere" } }); module.exports = mongoose.
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var LocationSchema = new Schema ({
geo: {
type: [Number],
required: true,
index: "2dsphere"
}
});
module.exports = mongoose.model("Location", LocationSchema);
我想找到新的地点:
Location.find({
geo: {
$nearSphere: [req.query.lat, req.query.lng],
$maxDistance: maxDistance
}
}, function(err, events) {
if (err) {
console.log(err);
res.status(500).json(err);
}
res.status(200).json(events);
});
{ [MongoError: Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0
Tree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query]
name: 'MongoError',
message: 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
'$err': 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 17007 }
该索引似乎已创建:
> db.system.indexes.find();
{ "v" : 1, "key" : { "geo" : "2dsphere" }, "name" : "geo_2dsphere", "ns" : "dev.events", "background" : true, "safe" : null, "2dsphereIndexVersion" : 2 }
虽然我在这里创建索引,但当我想查找附近的位置时,会出现以下错误:
Location.find({
geo: {
$nearSphere: [req.query.lat, req.query.lng],
$maxDistance: maxDistance
}
}, function(err, events) {
if (err) {
console.log(err);
res.status(500).json(err);
}
res.status(200).json(events);
});
{ [MongoError: Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0
Tree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query]
name: 'MongoError',
message: 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
'$err': 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 17007 }
您已经在位置模式中定义了“geo”,并且正在使用
loc : {
$nearSphere: [req.query.lat, req.query.lng],
$maxDistance: maxDistance
}
换成
geo : {
$nearSphere: [req.query.lat, req.query.lng],
$maxDistance: maxDistance
}
从Mongo使用[经度,纬度],这里您使用的是相反的。在将parse数据库迁移到AWS后,我遇到了类似的问题。 我已经把问题解决了
db.prod.createIndex({ "location": "2d" })
其中prod是我的集合名称,location是存储地理位置(GeoPoint)的列的名称
可在上找到与讨论相关的&geoNear,确保查询中的字段名与架构中的名称匹配。对于我来说,这个错误出现了,因为我的模式有“loc”,而查询正在搜索“lastLocation”
var mongoQuery = User.find({
lastLocation: {
$near: coordinates,
$maxDistance: maxDistance
}
}).limit(limit);
var UserSchema = new mongoose.Schema({
//...
lastLocation: {
type: [Number],
index: '2d'
}
});
嗨,这不是问题所在。仍然会得到相同的错误。根据mongodb查询$nearSphere:[req.query.lng,req.query.lat],您先给出经度,然后给出纬度。希望它能起作用