it seems like using $or with a sort does a full table scan and avoids my indexes on title and keywords how can I get it to use my two indexes when using an $or query?
this query uses both the title and keywords index
db.tasks.find({$or: [{keywords: /^japan/}, {title:/^japan/}]})
this does a full table scan and uses my index total_-1
db.tasks.find({$or: [{keywords: /^japan/}, {title:/^japan/}]}).sort({total:-1})
while queries against keywords or title with a sort do use the indexes on keywords or title respectively.
db.tasks.find({title:/^japan/}).sort({total:-1})
db.tasks.find({keywords:/^japan/}).sort({total:-1})
Sorting and indexes in Mongo are a complex topic. Mongo also has a special error that prevents you from doing a sort without an index if you have too many items. So it’s good that you’re asking about indexes, because an un-indexed sort will eventually start failing.
There is a bug in JIRA that seems to cover your issue, however there are some extra details to consider.
The first thing to note are your last queries:
These queries will fail eventually because you are only indexing on
titlenot ontitle/total. Here’s a script that will demonstrate the problem.So if you plan to query & sort on
titleandtotal, then you need an index on both, in that order:The JIRA bug I listed above is for something like the following:
Yours is slightly different, but it will encounter the same problem. Even if you have both indexes on
title/totalandkeyword/totalMongoDB will not be able to use indexes optimally.