September 2018
Beginner to intermediate
192 pages
4h 37m
English
Compound indexes are useful when you wish to create an index on more than one field. To illustrate the use of this type of index, we will turn our attention again to the sweetscomplete.purchases collection. In this example, we issue a query which gives us a list of customers outside of the United States:
db.purchases.find( {"customer.country": {$not:/US/}}, {_id:0,"customer.name":1,"customer.state_province":1,"customer.country":1} ).sort( {"customer.country":1,"customer.state_province":1,"customer.name":1} );
When we tack the explain("executionStats") method to the query, we learn that this query takes 18 milliseconds to complete (only a fragment of the entire dump is shown):
We can now create an index on the fields that ...
Read now
Unlock full access