Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, given those queries, here are the indexes I would create:</p> <pre><code>db.events.createIndex({date: 1, category: 1}) db.events.createIndex({date: 1, keyword: 1}) </code></pre> <p>Either of these queries can be used for queries by <code>date</code> only, and either can be used for <code>date</code> + <code>category</code> + <code>keyword</code>. Which one is chosen in the last case will depend on the selectivity of the two fields and the particular query in question.</p> <p>You may also want an index on <code>date</code> 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.</p> <p>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 <a href="http://www.mongodb.org/display/DOCS/Database+Profiler" rel="nofollow">database profiler</a> to assist in collecting this information, possibly with a tool like <a href="http://late.am/post/2011/09/22/professor-a-mongodb-profile-viewer" rel="nofollow">Professor</a> (#shamelessplug) to assist in understanding the results.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload