Fastest way to sort on multiple count?

Hello @eddy_turbox,

You can sort on multiple fields. This sort operation can be supported by an index on multiple fields. An index on multiple fields is referred as a Compound Index. Also, refer this in the documentation for examples and some related scenarios:


It is better to sort the data before it is retrieved into your application. The query filter and sort and any other operations are applied on the database server and you get the result in a single call. Indexes can be defined to support the query filter and sort operations. With proper study and planning you can possibly use an existing index or a compound index or modify an existing index to support your queries with sort.

You can generate a Query Plan for a query and see how an index is applied for the filter and sort operations. The db.collection.explain method can be used for this purpose.

2 Likes