【发布时间】:2022-11-02 00:17:15
【问题描述】:
我有一个 MongoDB 集合,其中包含地理编码信息。在某些情况下,我希望允许包含不在定义的地理区域内的促销项目。下面是一个查询示例。
大约需要 8500 毫秒
{
"$and": [{
"After.Start": {
"$gte": ISODate("2022-10-28T00:00:00Z")
}
},{
"$or": [{
"LongLat": {
"$geoWithin": {
"$centerSphere": [[-111.888221740722, 40.760311126708999], 0.012616093290458801]
}
}
}, {
"ByPass": {
"$in": ["5162e"]
}
}
]
}
]
}
如果我将其作为两个单独的查询运行,我将无法添加其他条件或对其进行正确排序或分页,但它运行得更快。
~61ms
{
"$and": [{
"After.Start": {
"$gte": ISODate("2022-10-28T00:00:00Z")
}
},{
"LongLat": {
"$geoWithin": {
"$centerSphere": [[-111.888221740722, 40.760311126708999], 0.012616093290458801]
}
}
}
]
}
~1ms
{
"$and": [{
"After.Start": {
"$gte": ISODate("2022-10-28T00:00:00Z")
}
},{
"ByPass": {
"$in": ["5162e"]
}
}
]
}
我在所有三个字段上都有非复合索引,LongLat 上的索引是 2dsphere。 我还有一个关于 LongLat 和“After.Start”的复合索引。有没有不同的方法可以索引它或者存储数据以添加旁路。
【问题讨论】:
标签: mongodb indexing mongodb-query