MongoDB Indexing : Part 1


The Basics

Lets say you have a statement like

db.foo.find({x:2})

What does the server do in order to find this document?  It does an innocuous  for-each loop looking for the values, document by document ( assuming the documents were placed contiguously and there was no other logic involved in storing the document) until it finds the value (trying to make a point so I am over simplifying it).

Obviously what I have just stated is a very slow & inefficient way for searching the document. The solution is to create an Index ( as in all other modern databases). Each document has its own location on disk and an index logically holds mapping to those locations from field values.

Continue reading

Advertisements