September 2018
Beginner to intermediate
192 pages
4h 37m
English
Multi-key indexes (https://docs.mongodb.com/manual/core/index-multikey/#multikey-indexes) are needed if you are dealing with documents with array fields. As an example, let's assume that sweetscomplete.customers now contains a new field called purch_history, with a list of purchase dates. We wish to generate a count of customers who have purchased items in June 2018. Granted we could generate the same information by scanning the purchases collection, but to illustrate the need for a multi-key index, let's examine this sample query:
db.customers.find( { purch_history: { $elemMatch: { $regex: /^2018-06/ } } } ).count();
As you can see, from the query, the purch_history array is scanned using the $elemMatch array operator. ...
Read now
Unlock full access