我有以下架構,我在地理座標上有2d索引。
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);
});
該指數似乎要創建:
> db.system.indexes.find();
{ "v" : 1, "key" : { "geo" : "2dsphere" }, "name" : "geo_2dsphere", "ns" : "dev.events", "background" : true, "safe" : null, "2dsphereIndexVersion" : 2 }
雖然我在這裏創造一個指標,我得到時,我想下面的錯誤找到附近的地點:
{ [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 }
嗨,這不是問題。仍然得到相同的錯誤。 – user2500558
根據mongodb查詢$ nearSphere給出第一個經度和緯度:[req.query.lng,req.query.lat]。希望它會起作用 –