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 7712631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:40:50+00:00 2026-06-01T01:40:50+00:00

I need some advice in creating and ordering indexes in mongo. I have a

  • 0

I need some advice in creating and ordering indexes in mongo.

I have a post collection with 5 properties:

Posts

  • status
  • start date
  • end date
  • lowerCaseTitle
  • sortOrder

Almost all the posts will have the same status of 1 and only a handful will have a rejected status. All my queries will filter on status, start and end dates, and sort on sortOrder. I also will have one query that does a regex search on the title.

Should I set up a compound key on {status:1, start:1, end:1, sort:1}? Does it matter which order I put the fields in the compound index – should I put status first in the compound index since it’s the most broad? Is it better to do a compound index rather than a single index on each property? Does mongo only use a single index on any given query?

Are there any hints for indexes on lowerCaseTitle if I’m doing a regex query on that?

sample queries are:

db.posts.find({status: {$gte:0}, start: {$lt: today}, end: {$gt: today}}).sort({sortOrder:1})

db.posts.find( {lowerCaseTitle: /japan/, status:{$gte:0}, start: {$lt: today}, end: {$gt: today}}).sort({sortOrder: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-01T01:40:51+00:00Added an answer on June 1, 2026 at 1:40 am

    That’s a lot of questions in one post 😉 Let me go through them in a practical order :

    • Every query can use at most one index (with the exception of top level $or clauses and such). This includes any sorting.
    • Because of the above you will definitely need a compound index for your problem rather than seperate per-field indexes.
    • Low cardinality fields (so, fields with very few unique values across your dataset) should usually not be in the index since their selectivity is very limited.
    • Order of the fields in your compound index matter, and so does the relative direction of each field in your compound index (e.g. “{name:1, age:-1}”). There’s a lot of documentation about compound indexes and index field directions on mongodb.org so I won’t repeat all of it here.
    • Sorts will only use the index if the sort field is in the index and is the field in the index directly after the last field that was used to select the resultset. In most cases this would be the last field of the index.

    So, you should not include status in your index at all since once the index walk has eliminated the vast majority of documents based on higher cardinality fields it will at most have 2-3 documents left in most cases which is hardly optimized by a status index (especially since you mentioned those 2-3 documents are very likely to have the same status anyway).

    Now, the last note that’s relevant in your case is that when you use range queries (and you are) it’ll not use the index for sorting anyway. You can check this by looking at the “scanAndOrder” value of your explain() once you test your query. If that value exists and is true it means it’ll sort the resultset in memory (scan and order) rather than use the index directly. This cannot be avoided in your specific case.

    So, your index should therefore be :

    db.posts.ensureIndex({start:1, end:1})
    

    and your query (order modified for clarity only, query optimizer will run your original query through the same execution path but I prefer putting indexed fields first and in order) :

    db.posts.find({start: {$lt: today}, end: {$gt: today}, status: {$gte:0}}).sort({sortOrder:1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice, what is the rule of the thumb when creating Rails
Experts - I need some advice in the following scenario. I have a configuration
I have been playing with Compact Framework lately and i need some advice. I
I need some expect advice on how to handle the following:- I have a
I need some advice on creating a thumbnail cache for amazon s3. I need
I need some advice on creating a syntax highligher for java application using swing.
I'm creating a text-based browser game and need some advice for django model structure.
Need some advice on creating a floating menu that behaves very similar to what
I need some help creating a query for my mySQL database. I have recently
I need some advice as to how I easily can separate test runs for

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.