Mongodb 在$geoNear查询之后进行筛选时的性能
我有一个MongoDB集合,其中包含一个位置(GeoJSON点)和其他要过滤的字段Mongodb 在$geoNear查询之后进行筛选时的性能,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个MongoDB集合,其中包含一个位置(GeoJSON点)和其他要过滤的字段 { "Location" : { "type" : "Point", "coordinates" : [ -118.42359, 33.974563 ] }, "Filters
{
"Location" : {
"type" : "Point",
"coordinates" : [
-118.42359,
33.974563
]
},
"Filters" : [
{
"k" : 1,
"v" : 5
},
{
"k" : 2,
"v" : 8
}
]
}
我的查询使用聚合函数,因为它执行一系列过滤、排序、分组等操作。。。过滤的第一步是我在执行地理位置接近操作时遇到问题
$geoNear: {
spherical: true,
near: [-118.236391, 33.782092],
distanceField: 'Distance',
query: {
// Filter by other fields.
Filters: {
$all: [
{ $elemMatch: { k: 1 /* Bedrooms */, v: 5 } }
]
}
},
maxDistance: 8046
},
对于索引,我尝试了两种方法:
方法#1:创建两个单独的索引,一个包含位置字段,另一个包含我们随后筛选的字段。这种方法很慢,我收集的数据很少,在5英里半径范围内查询需要3秒以上的时间
db.ResidentialListing.ensureIndex( { Location: '2dsphere' }, { name: 'ResidentialListingGeoIndex' } );
db.ResidentialListing.ensureIndex( { "Filters.k": 1, "Filters.v": 1 }, { name: 'ResidentialListingGeoQueryIndex' } );
方法#2:创建一个包含位置和我们筛选的其他字段的索引。创建索引从未完成,因为它生成了大量关于“插入地理对象生成了大量键”的警告
如果我只执行$geoNear操作,不尝试在之后进行查询,那么geo索引本身似乎工作正常,它将在60毫秒内执行。然而,当我尝试在之后查询其他字段时,它就会变慢。任何关于如何正确设置查询和索引以使其性能良好的想法都将不胜感激
db.ResidentialListing.ensureIndex( { Location: '2dsphere', "Filters.k": 1, "Filters.v": 1 }, { name: 'ResidentialListingGeoIndex' } );