我有以下架构,我在地理坐标上有一个二维索引.
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);
Run Code Online (Sandbox Code Playgroud)
我想找到新的位置:
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);
});
Run Code Online (Sandbox Code Playgroud)
该索引似乎已创建:
> db.system.indexes.find();
{ "v" : 1, "key" : { "geo" : "2dsphere" }, "name" : "geo_2dsphere", "ns" : "dev.events", "background" : true, "safe" : null, "2dsphereIndexVersion" : 2 …Run Code Online (Sandbox Code Playgroud)