我正在使用 mongoDB
存储多边形的集合,并使用 $geoIntersects
查询来查找特定点在哪个多边形中。
我的 mongoose
Schema
如下所示:
var LocationShema = mongoose.Schema({
name: String,
geo: {
type: {
type: String
},
coordinates: []
}
});
LocationShema.index({geo: '2dsphere'});
module.exports = mongoose.model('Location', LocationShema);
所以,每个元素都是一个多边形。我添加了2dsphere
索引,希望查询速度更快,并且整个集合存储在内存中。不幸的是,对于我的用例,大约20个查询需要花费600毫秒
,这太长了。
我的查询看起来像这样:
Location.find({
geo: {
$geoIntersects: {
$geometry: {
type: 'Point',
coordinates: [pos.lng, pos.lat]
}
}
}
},...)
有没有办法让这个运行更快?我能否强制MongoDB将整个集合缓存在数据库中(因为该集合从不改变)?有没有办法检查集合是否实际存储在内存缓存中?
此外,是否有任何替代方案可供使用(例如:库或其他东西),可以实现快速地地理空间查询?
explain()
吗? - KRONWALLED