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.