Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:43:01+00:00 2026-06-01T07:43:01+00:00

it seems like using $or with a sort does a full table scan and

  • 0

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})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:43:02+00:00Added an answer on June 1, 2026 at 7:43 am

    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:

    db.tasks.find({title:/^japan/}).sort({total:-1})
    db.tasks.find({keywords:/^japan/}).sort({total:-1})
    

    These queries will fail eventually because you are only indexing on title not on title/total. Here’s a script that will demonstrate the problem.

    > db.foo.ensureIndex({title:1})
    > for(var i = 0; i < 100; i++) { db.foo.insert({title: 'japan', total: i}); }
    > db.foo.count()
    100
    > db.foo.find({title: 'japan'}).sort({total:-1}).explain()
    ... uses BTreeCursor title_1
    > // Now try with one million items
    > for(var i = 0; i < 1000000; i++) { db.foo.insert({title: 'japan', total: i}); }
    > db.foo.find({title: 'japan'}).sort({total:-1}).explain()
    Sat Mar 31 05:57:41 uncaught exception: error: {
            "$err" : "too much data for sort() with no index.  add an index or specify a smaller limit",
            "code" : 10128
    }
    

    So if you plan to query & sort on title and total, then you need an index on both, in that order:

    > db.foo.ensureIndex({title:1,total:1})
    > db.foo.find({title: 'japan'}).sort({total:-1}).explain()
    {
            "cursor" : "BtreeCursor title_1_total_1 reverse",
    ...
    

    The JIRA bug I listed above is for something like the following:

    > db.foo.find({$or: [title:/^japan/, title:/^korea/]}).sort({total:-1})
    

    Yours is slightly different, but it will encounter the same problem. Even if you have both indexes on title/total and keyword/total MongoDB will not be able to use indexes optimally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with using the prefix, seems like whenever I use the
I have an entity and it seems like using DateTime as the Id would
I would like to animate a ScatterView Control using Expression Blend However, it seems
Seems like cuke doesn't show the full error message (at least when problem occurs
Seems like a standard approach for an ioc when given a scenario like (C#
Seems like this should be obvious, but how do I send arrow key presses
Seems like this should be simple, but powershell is winning another battle with me.
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name
Seems like it would be better if you did commit followed by merge. I'm
Does .Net 4 (or any prior version) perform any sort of optimization on longer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.