0

This question on MongoDB compound index:

Below is a sample document in a given collection test. { a : 5, b : 3, c: 2, d : 1 } Given a compound index { a: 1, b:1, c:1, d:1}, Which of the below query will not use in-memory sorting? Select all valid. A) db.test.find( { a: 5, b: 3 } ).sort( { c: 1, d : 1 } ) B) db.test.find( { a: 5, b: 3 } ).sort( { a: 1} ) C) db.test.find( { a: 5, b: 3 } ).sort( { a: 1, b: 1, c: 1 } ) D) db.test.find( { a: 5, b: 3 } ).sort( { c: 1 } ) 

My understanding is A, B, C, D will all be able to use the compound index, without the need for in-memory sorting. However I've asked around, even asked different AI agents, but all have different answers. Would anyone take a shot at answering this?

3
  • Why do you like to avoid in-memory sorting? Commented Oct 30 at 18:14
  • In short: MongoDB uses an index to return documents in the requested sort order without loading all results into memory. It’s fast, scalable, and efficient. If no suitable index exists for the sort order, MongoDB retrieves all matching documents, loads them into RAM, and sorts them in memory before returning results. It could slow down system or crash under large result sets. Commented Oct 30 at 18:21
  • Have a look at Sort on Multiple Fields, this should answer your question. Commented Oct 31 at 6:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.