Why can't the index {active: 1, birthday: -1, name: 1} be used for sorting by birthday?

Prepare for the MongoDB Associate Developer Exam with flashcards and practice multiple choice questions. Each question includes hints and explanations to ensure you're ready for test day.

The correct choice emphasizes a key principle of how MongoDB utilizes compound indexes for sorting and querying. In a compound index, the order of fields in the index matters significantly. The index is structured to optimize searches and sorting based on the order of the specified fields.

In this case, when sorting by the birthday field, the index {active: 1, birthday: -1, name: 1} cannot be utilized effectively because the first field in the index is active. MongoDB uses the fields in the order they are defined in the index, and since the sorting operation requires the birthday field, which comes after active in the index structure, the database cannot leverage this index efficiently for that specific sorting criteria.

To sort by birthday, MongoDB would typically look for an index where birthday is either the first field or at least has a more suitable prefix arrangement that allows for efficient sorting operations. The inability to match the prefix of the index is crucial because indexes are traversed hierarchically, starting from the first field listed in the index definition.

The other options do not appropriately capture the issue at hand. A non-existent index does not apply here since the index does exist. While it's true that it should match

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy