mongodb 中 OR+Sort 查询的复合索引 [英] Compound indices for OR+Sort query in mongodb
本文介绍了mongodb 中 OR+Sort 查询的复合索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于这个 OR 查询:
For this OR query:
db.messages.find({ $or: [ { to: { $ne: null }, from: "xyz" }, { to: "xyz" } ] }).sort({_id: -1}).limit(50)
具有以下索引:
{to:1, from: 1, _id:-1} and {from:1, to:1, _id:-1}
mongo 总是进行全面扫描.
mongo is always doing a full scan.
我希望 mongo 可以使用这两个索引并合并结果.
I was hoping that mongo could use these two indices and merge the results.
我是否需要将其拆分为两个查询(每个 OR 子句一个)并自行合并?或者是否有其他索引可以提供帮助?
Do I need to split this into two queries (one for each OR clause) and merge myself? Or is there some other index that can help?
推荐答案
这是一个已知问题,https://jira.mongodb.org/browse/SERVER-1205,如果它对您的用例非常重要,您可以投票.
This is a known issue, https://jira.mongodb.org/browse/SERVER-1205, which you can vote for if it is very important for your use case.
这篇关于mongodb 中 OR+Sort 查询的复合索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文