Database Reference
In-Depth Information
Introducing indexes
In order to boost the database's performance, we can use indexes to categorize the records
of a collection. Thus, the database engine can perform read/write queries faster than before.
Note
Indexes support the efficient execution of queries in MongoDB. Without indexes, Mon-
goDB must scan every document in a collection to select those documents that match the
query statement. These collection scans are inefficient because they require mongod to
process a larger volume of data than an index for each operation. For more information on
indexes, please visit http://docs.mongodb.org/manual/core/indexes-introduction/ .
If you frequently work with specific fields in a collection, we recommend that you define
an index for them to improve performance. For instance, if you sort a collection based on
certain fields regularly, you can make that field an indexed field so that the database engine
can perform the query much faster.
To define an index for a specific field, you can use following command:
db.news.ensureIndex({ title: 1 })
The preceding command creates an index for the title field of the news collection.
After creating the index field, if you issue the following command, you will get the result
faster than before:
db.news.find().sort({ title: -1 })
Note
It doesn't matter if you sort the document's field in the ascending or descending order be-
cause the database engine can read the index in both directions. It is worth mentioning that
it's impossible to tell you everything about index features in one section. So, for more in-
formation on the concept of indexes and tutorials, please visit http://docs.mongodb.org/
manual/indexes/ .
Search WWH ::




Custom Search