开发者

Compound indices for OR+Sort query in mongodb

For this OR query:

db.messages.find({ $or: [ { to: { $ne: null }, from: "xyz" }, { to: "xyz" } ] }).sort({_id: -1}).limit(50)

with following indices:

{to:1, from: 1, _id:-1} and 开发者_如何学编程{from:1, to:1, _id:-1}

mongo is always doing a full scan.

I was hoping that mongo could use these two indices and merge the results.

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜