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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:26:49+00:00 2026-05-30T19:26:49+00:00

I’m new to MongoDB and trying to make sure I set up the proper

  • 0

I’m new to MongoDB and trying to make sure I set up the proper indexes. I’ve seen similar questions about composite indexes here but none that exactly cover the situation I’m in.

Note: I’m using Rails 3.2 and Mongoid.

I have a collection of Events that are always going to be sorted (and often searched on) date, but generally with another parameter as well. For example, I might want to find the Events that match a particular set of categories within a certain date range; or I might want to find the Events that match a particular person within a certain date range. The types of searches will be:

  1. Always by date (or at least sorting by date)
  2. Often by category
  3. Sometimes additionally by [person, venue, or keyword]

The first solution I came up with was multiple composite keys that all start with date and category, like so:

class Event
...

index ([
    [:date, Mongo::DESCENDING], 
    [:category_id, Mongo::ASCENDING]
    ["people.person_id", Mongo::ASCENDING]
  ])
index ([
    [:date, Mongo::DESCENDING], 
    [:category_id, Mongo::ASCENDING]
    [:venue_id, Mongo::ASCENDING]
  ])
index ([
    [:date, Mongo::DESCENDING], 
    [:category_id, Mongo::ASCENDING]
    [:keywords, Mongo::ASCENDING]
  ])

But it seems a little funny to me to keep overlapping the “date + category_id” index, and also what about the cases when I’m not searching on category_id?

UPDATE: dcrosta asked what kind of queries would be running, and how frequently. Without knowing exactly, I can guess that it would look something like the following:

Very frequent:

  • by date
  • by date + category
  • by date + keyword
  • by date + category + keyword

Somewhat frequent:

  • by date + person
  • by date + venue

Less frequent:

  • by date + category + venue
  • by date + category + person
  • 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-05-30T19:26:50+00:00Added an answer on May 30, 2026 at 7:26 pm

    OK, given those queries, here are the indexes I would create:

    db.events.createIndex({date: 1, category: 1})
    db.events.createIndex({date: 1, keyword: 1})
    

    Either of these queries can be used for queries by date only, and either can be used for date + category + keyword. Which one is chosen in the last case will depend on the selectivity of the two fields and the particular query in question.

    You may also want an index on date by itself, which will serve as a catch-all for the remaining queries. Whether or not this is going to help much depends on the volume of data and exactly what “somewhat frequent” means, exactly.

    More generally speaking, and addressing your initial question, indexes in MongoDB, like any database, will increase the performance of queries (for those queries which they match), at the cost of slightly degrading the performance of updates/inserts/deletes (since the index must be modified along with the underlying data). My approach is to build indexes for those queries which I know will be either very costly or very frequent, and then test using a realistic distribution of load (i.e. a realistic number and frequency of queries and updates/inserts/deletes) to see what other queries are more costly than you expect. You can use the database profiler to assist in collecting this information, possibly with a tool like Professor (#shamelessplug) to assist in understanding the results.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

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.