Indexes
Indexing ensures faster reads. However, indexes have a hit on writes, that is, creation and updating of documents. There are various types of indexes supported by MongoDB. We have already seen these various types of indexes earlier.
Tip
Indexes are not automatically created. When we specify the index in our model, we need to run the rake db:mongoid:create_indexes
command to create them. To get a list of database tasks that Mongoid supports, issue the rake –T db:mongoid
command.
The following table gives a quick summary of the various options that are available for index creation:
Name |
Example |
Description |
---|---|---|
normal |
index({name: 1}) index({name: 1}, {name: 'n' }) index({"address.location" => 1}) |
This is the typical index that gets created. The index name gets saved as a concatenation of the field names unless the |
unique drop_dups |
index({name... |