ALL COVERED TOPICS

NoSQL Benchmarks NoSQL use cases NoSQL Videos NoSQL Hybrid Solutions NoSQL Presentations Big Data Hadoop MapReduce Pig Hive Flume Oozie Sqoop HDFS ZooKeeper Cascading Cascalog BigTable Cassandra HBase Hypertable Couchbase CouchDB MongoDB OrientDB RavenDB Jackrabbit Terrastore Amazon DynamoDB Redis Riak Project Voldemort Tokyo Cabinet Kyoto Cabinet memcached Amazon SimpleDB Datomic MemcacheDB M/DB GT.M Amazon Dynamo Dynomite Mnesia Yahoo! PNUTS/Sherpa Neo4j InfoGrid Sones GraphDB InfiniteGraph AllegroGraph MarkLogic Clustrix CouchDB Case Studies MongoDB Case Studies NoSQL at Adobe NoSQL at Facebook NoSQL at Twitter

NAVIGATE MAIN CATEGORIES

Close

MongoDB Indexes and Indexing

One of the features that sets MongoDB aside from other NoSQL databases is dynamic queries, something that most developers coming from relational databases are very used to. As you could expect, to make these dynamic queries fast, you’ll need to define some indexes.

Kyle Banker (10gen) has a ☞ great generic introduction to indexes, covering subjects like simple and compound indexes, selectivity, limitations:

Now, to be clear, I’m not talking about how to create an index. That’s easy. The trouble runs much deeper. It’s knowing how indexes work and having the intuition to create the best indexes for your queries and your data set. Lacking this intuition, your production database will eventually slow to a crawl, you’ll upgrade your hardware in vain, and when all else fails, you’ll blame both gods and men.

But how do you define indexes in MongoDB? You can find the answer in the ☞ official docs or for quick reference you can use the slides below:

Before trying MongoDB indexes yourself, you could also check what others have written. Klaus Lehner’s ☞ post is sort of his learning notes about MongoDB indexes:

Suddenly, only 30 documents are touched (our result set), and the time drops down to 1ms.

What you can also see there, is which cursor MongoDB used to retrieve the result. In the first case it was the BasicCursor, in the second the BtreeCursor for our index price_1 (which means ascending). As a rule of a thumb, ensure that for your queries always a BtreeCursor is used.

Now, it’s time to have some fun with MongoDB queries and indexes!

Update: I’ve just realized I haven’t mentioned MongoDB geospatial indexes, a feature available since MongoDB 1.4 that made MongoDB attractive for solutions like the multiplayer online Scrabble Scrabb.ly and Foursquare.

Update: Check also MongoDB Indexing: An Optimization Primer

Original title and link: MongoDB Index and Indexing (NoSQL databases © myNoSQL)