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...