简体   繁体   English

替代mongo汇总查询

[英]alternate mongo aggregate query

I have an aggregate mongodb query 我有一个汇总的mongodb查询

db.collection.aggregate([
    {$match: {a: "a1", b: "b1"}},{$group: {...}}, 
    {$sort: {...}}, {$limit: {10}}
])

I have a compound index on a and b, and the query is fast. 我在a和b上有一个复合索引,查询速度很快。

However when I change the match to 但是当我将比赛更改为

 $match: {$or: [{a: "a1", b: "b1"}, {a: "a2", b: "b2"}]}

the query becomes quite slow and it doesn't seem to use any indices. 查询变得非常缓慢,并且似乎没有使用任何索引。

Is there any way to either 1) Rewrite the query to use the compound index (as a single query) or 2) Force it to use the index? 有什么方法可以1)重写查询以使用复合索引(作为单个查询),或2)强制其使用索引?

I would try adding a sort step to the beginning of the aggregation to see if it forces the use of your index. 我会尝试在聚合的开头添加一个排序步骤,以查看它是否强制使用索引。

db.collection.aggregate([
    {$sort: {a:1, b:1}},
    {$match: {$or: [{a: "a1", b: "b1"}, {a: "a2", b: "b2"}]}},
    ...
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM