Sorting using Compound Indexes

We can use the sort() function of MongoDB on the created index as indexes contain ordered records, MongoDB can obtain the results of a sort from an index with which are Sort expression matches(matching using prefix). If MongoDB cannot use an index to obtain the sort order it performs a blocking sort operation on the data in which it consumes and processes all input documents to the sort before returning results.

If the sort keys match an index prefix, MongoDB can use the index to sort the query results. A prefix is a subset that consists of one or more keys of the index key pattern.

For e.g. suppose we create a compound index by 

db.data.createIndex({a:1, b: -1, c:1})

Then we have the following prefixes on which our created index is used for sorting –

{a:1}
{a:1,b:-1}
{a:1,b-1,c:1}
Example

prefix

db.data.find().sort({a: 1})

{a: 1}

db.data.find().sort({a: -1})

{a: 1}

db.data.find().sort({a: 1, b: -1})

{a: 1, b: -1}

db.data.find().sort({a: -1, b: 1})

{a: 1, b: -1}

db.data.find().sort({a: 1, b: -1, c: 1})          

         {a: 1, b: -1, c: 1}

So for all the examples in the above table MongoDB will use our created index but not on db.data.find().sort({a: -1, b: -1}) or db.data.find().sort({a: 1, b: 1}) as they don’t match any prefix for these two MongoDB has to perform blocking sort.

We can also use non-prefix like {b: -1, c: 1} for sorting but for this, we have to put equality on the preceding prefix key .for e.g.

db.data.find({a: 6}).sort({b: -1, c: 1})

Here we have put an equality condition on key ‘a’ and now it will use prefix {a: 1, b: -1, c: 1} 

Example:

db.products.find().sort({manufacturer:1,price:-1}) 

In the above example since our sort key pattern has manufacture:1, price:-1 which is similar to the second index manufacture_1_price_-1 key thus this index will be used to obtain result instead of doing sort again. 


MongoDB – Compound Indexes

MongoDB provides indexing for efficient execution of queries without indexes MongoDB has to search every document to match the query which is highly inefficient. Indexes are easy to traverse and store sorted documents according to the specified fields. Unlike single field index in which indexing is done on a single field, Compound Indexes does indexing on multiple fields of the document either in ascending or descending order i.e. it will sort the data of one field, and then inside that it will sort the data of another field. Or in other words, compound indexes are those indexes where a single index field contains references to multiple fields. In MongoDB, the compound index can contain a single hashed index field, if a field contains more than one hashed index field then MongoDB will give an error.

Similar Reads

How to create a compound Index?

In MongoDB, we can create compound index using createIndex() method....

Sorting using Compound Indexes

We can use the sort() function of MongoDB on the created index as indexes contain ordered records, MongoDB can obtain the results of a sort from an index with which are Sort expression matches(matching using prefix). If MongoDB cannot use an index to obtain the sort order it performs a blocking sort operation on the data in which it consumes and processes all input documents to the sort before returning results....

Contact Us