nerdexam
MongoDB

C100DBA · Question #111

You have the following index on the toys collection: { "manufacturer" : 1, "name" : 1, "date" : -1 } Which of the following is able to use the index for the query? Check all that apply.

All three options - A, B, and C - can use the index. MongoDB's query planner does not care about the order in which fields appear in the find() predicate; it cares whether the queried fields match a left-prefix of the compound index. Here, the index prefix chain is manufacturer…

Performance Tuning

Question

You have the following index on the toys collection:

{ "manufacturer" : 1, "name" : 1, "date" : -1 } Which of the following is able to use the index for the query? Check all that apply.

Options

  • Adb.toys.find( { name : "Big Rig Truck", date : "2013-02-01", manufacturer : "Tanko"
  • Bdb.toys.find( { manufacturer : "Matteo", name : "Barbara", date : "2014-07-02" } )
  • Cdb.toys.find( { date : "2015-03-01", manufacturer : "Loggo", name : "Brick Set" } )

Explanation

All three options - A, B, and C - can use the index.

MongoDB's query planner does not care about the order in which fields appear in the find() predicate; it cares whether the queried fields match a left-prefix of the compound index. Here, the index prefix chain is manufacturermanufacturer, namemanufacturer, name, date. All three queries supply values for all three indexed fields (manufacturer, name, and date), so all three fully match the index regardless of how the fields are ordered inside the {} braces. There are no distractors here - this is a question specifically designed to test whether you mistakenly believe field order in the query document must match field order in the index definition.

Memory tip: Think of a compound index like a phone book sorted by Last Name → First Name → Street. You can look someone up by last name alone, last+first, or all three - but the page you write the search query on doesn't need to list them in that exact order; the index structure itself enforces the sort order.

Topics

#compound index#index prefix#query planning#index utilization

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice