Recall that an index on a database field is a tree structure that greatly increases the efficiency of queries on that field. In Chapter 5, Relational Databases, we described the B-tree data structure (see Figure 5-18) that is the common mechanism for implementing database indexes.
Like relational database systems, Mongo also supports indexing. As an example, suppose that our books
collection contains 1,000,000 documents, one for each book. Further, suppose that this query is executed:
db.books.find({year:{"$gte":1924,"$lt":1930}})
This would list all books published from 1924 to 1930. If the year filed is indexed, the response would be instantaneous. Otherwise, every one of the 1,000,000 documents would have to be examined.
The required _id
field in each collection is indexed automatically. This is a unique index, which means that it prevents the insertion of any document with the same _id
value as one already in the collection.
To index any other field, use the db.collection.createIndex(key)
method, as shown in Figure 10-19. The value 1
indicates that the index is in an ascending order of the specified field values.
As in relational databases, indexes take up a lot of space, and they can slow the process of insertions and deletions. So, it's probably not a good idea to index every field. The best strategy is to create indexes on just the most commonly searched fields. For example, in our library
database, we would probably want to create indexes on the books.year
field, the books.author.author_id
field, the books.title
field, and the publishers.name
field.
MongoDB also supports compound indexes. The general syntax is:
db.collection.createIndex({<field1>: <type>, <field2>: <type2> ... })
For example, db.books.createIndex({year:1, title:1})
would create a two-dimensional compound index, indexing first on the year
field, and second on the title
field within each year. That index would facilitate frequent queries like this:
db.books.find({}, {year:1, title:1, publisher:1}).sort({year:1})
We can also index geospatial database collections. MongoDB supports two specialized geospatial indexes: one for planar two-dimensional geometric data, and one for spherical geometric data. The former is useful in graphics applications, while the latter applies to geographic locations on the surface of the earth. To see why these two contexts are different, recall that the sum of the angles of a plane triangle is always 180˚; but in a spherical triangle, all three angles could be right angles, making the sum 270˚. Think of a spherical triangle with its base on the equator and its two sides on meridians down from the North Pole.