Databases Reference
In-Depth Information
Sometimes, you may want to drop and create new indexes instead of rebuilding the ones that exist.
Indexes can be dropped with the dropIndex command:
db.ratings.dropIndex({ movie_id:-1 });
Available for
download on
Wrox.com
movielens_indexes.txt
This command drops the descending order movie_id index. You can also drop all indexes if need
be. All indexes (except the one of the _id fi eld) can be dropped as follows:
db.ratings.dropIndexes();
Available for
download on
Wrox.com
movielens_indexes.txt
Compound and Embedded Keys
So far, you have created indexes on only a single fi eld or property. It's also possible to create
compound indexes that involve multiple fi elds. For example, you may choose to create an index on
movie_id and ratings fi elds together. The command to create such an index is:
db.ratings.ensureIndex({ movie_id:1, rating:-1 });
Available for
download on
Wrox.com
movielens_indexes.txt
This creates a compound index on movie_id (ascending order sorted) and rating (descending
order sorted). You can create three more indexes out of the four total possible compound indexes
involving movie_id and rating . The four possibilities arise due to possible ascending and
descending order sorts of the two keys. The order of the sort can have an impact on queries that
involve sorting and range queries so keep the order in mind when you defi ne the compound indexes
for your collection.
A compound index involving movie_id and rating can be used to query for documents that are
matched on both these keys and the fi rst key (that is, movie_id ) alone. When using this index to
fi lter documents on the basis of movie_id alone, the behavior is similar to a single fi eld index on
movie_id .
Compound keys are not restricted to two keys. You can include as many keys as you like. A compound
index for movie_id , rating , and user_id can be created like so:
db.ratings.ensureIndex({ movie_id:1, rating:-1, user_id:1 });
Available for
download on
Wrox.com
movielens_indexes.txt
Search WWH ::




Custom Search