Creating and Listing Indexes
Indexes can be created by executing a createIndex()
command on a collection, as follows:
db.collection.createIndex( keys, options )
The first argument to the command is a list of key-value pairs, where each pair consists of a field name and sort order, and the optional second argument is a set of options to control the indexes.
In a previous section, you wrote the following query to find all the movies released in 2015, sort them in descending order of the number of awards won, and print the title and number of wins:
db.movies.find( Â Â Â Â { Â Â Â Â Â Â Â Â "year" : 2015 Â Â Â Â }, Â Â Â Â { Â Â Â Â Â Â Â Â "title" : 1, Â Â Â Â Â Â Â Â "awards.wins" : 1 Â Â Â Â } ).sort( Â Â Â Â {"awards.wins" : -1} )
As the query uses a...