Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound indices for OR+Sort query in mongodb

Tags:

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?

like image 574
tarkeshwar Avatar asked Oct 11 '22 12:10

tarkeshwar


1 Answers

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.

like image 189
dcrosta Avatar answered Nov 29 '22 21:11

dcrosta