How do I arrange my document names in a collection alphabetically and henceforth make my searches for a particular document faster? Say I have a million documents in a collection. I want to search for a single document as fast as possible and I believe doing it alphabetically is fastest. How do I do it?


The simplest way to do what you’re asking would be to use a well-considered compound index. This article from MongoDB explains it pretty well:

Compound indexes are indexes composed of several different fields. For example, instead of having one index on "Last name" and another on "First name", it is typically most efficient to create an index that includes both "Last name" and "First name" if you query against both of the names. Our compound index can still be used to filter queries that specify the last name only.

You can learn more about compound index creation from the MongoDB website and, if you enjoy books, Shannon Bradshaw goes into greater detail on this and other performance-related subjects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.