Database Reference
In-Depth Information
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "blog.authors", "name" : "_id_" }
The blog database does not have any user-defined indexes, but you can see the two indexes created
automatically for the _id field on your two collections: posts and authors . You don't have to do anything to create or
delete these identity indexes ; MongoDB creates and drops them whenever a collection is created or removed.
When you define an index on an element, MongoDB will construct an internal btree index, which it will use to
locate documents efficiently. If no suitable index can be found, MongoDB will scan all the documents in the collection
to find the records that satisfy the query.
Creating a Simple Index
MongoDB provides the ensureIndex() function for adding new indexes to a collection. This function begins by
checking whether an index has already been created with the same specification. If it has, then ensureIndex() just
returns that index. This means you can call ensureIndex() as many times as you like, but it won't result in a lot of
extra indexes being created for your collection.
The following example defines a simple index:
$mongo
>use blog
>db.posts.ensureIndex({Tags:1})
This example creates a simple ascending btree index on the Tags field. Creating a descending index instead
would require only one small change:
>db.posts.ensureIndex({Tags:-1})
To index a field in an embedded document, you can use the normal dot notation addressing scheme; that is, if
you have a count field that is inside a comments subdocument, you can use the following syntax to index it:
>db.posts.ensureIndex({"comments.count":1})
If you specify a document field that is an array type, then the index will include all the elements of the array as
separate index terms. This is known as a multi-key index, and each document is linked to multiple values in the index.
If you look back and examine the explain() output for our queries earlier, you can see mention of this there.
MongoDB has a special operator, all , for performing queries where you wish to select only documents that have
all of the terms you supply. In the blog database example, you have a posts collection with an element called Tags .
This element has all the tags associated with the posting inside it. The following query finds all articles that have both
the sailor and moon tags:
>db.posts.find({Tags:{$all: ['sailor', 'moon']}})
Without a multi-key index on the Tags field, the query engine would have to scan each document in the
collection to see whether either term existed and, if so, to check whether both terms were present.
Creating a Compound Index
It may be tempting to simply create a separate index for each field mentioned in any of your queries. While this may
speed up queries without requiring too much thought, it would unfortunately have a significant impact on adding and
removing data from your database, as these indexes need to be updated each time. It's also important to note that in
MongoDB 2.4 and earlier only one index will ever be used to fulfill the results of a query, so adding a number of small
indexes will not normally help query execution.
 
Search WWH ::




Custom Search