C100DBA practice question 22 of 25
Consider the following example document: { "_id": Objectld("5360c0a0a655a60674680bbe"), "user" "login": "irOn" "description": "Made of steel" "date"…
Full set: 130 questionsThe query will use the mylndex because the filter { "user.login": /Air./ } is a prefix-based regular expression on the first field of the compound index. This allows MongoDB to perform an index scan, which is more efficient than a collection scan. However, the index cannot be used to satisfy the sort order { "user.date": 1 } because the query's sort direction (ascending) is the opposite of the index's sort direction (descending) for that field, and the filter on the prefix field is a range scan (regex), not an equality match. Therefore, MongoDB will perform an in-memory sort after retrieving the documents using the index.
A. The sort is not optimized (scanAndOrder=true) because the sort order in the query (1) is the reverse of the index order (-1) for the user.date field.
C. A collection scan is avoided because the index on user.login can be used to satisfy the filter condition.
D. This is incorrect because option B is a correct statement describing the query's execution.
E. This is not a covered query because the projection { "user": 1 } returns the entire user subdocument, which includes fields not present in the index (e.g., description).
1. MongoDB Manual, "Use Indexes to Sort Query Results". Section: "Sort with a Different Order". It states: "However, if the query specifies a different sort direction for the keys, MongoDB cannot use the index for the sort."
2. MongoDB Manual, "Compound Indexes". Section: "Sort Order". It explains that for range scans on a prefix key, subsequent keys must match the index sort order to be used for sorting.
3. MongoDB Manual, "Covered Query". It specifies that for a query to be covered, all the fields in the query's filter and projection must be part of the index.
Keep practicing
Take the free C100DBA practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other C100DBA practice questions
- Question 1If the value of totalKeysExamined is 30000 and the value of totalDocsExamined is 0, which…
- Question 2The oplog (operations log) is a special capped collection that keeps a rolling record of …
- Question 3‘$set' sets the value of
- Question 4What does the totalKeysExamined field returned by the explain method indicate?
- Question 5Which mongodb tools allow us to work with our data in a human readable format?
- Question 6Which of the following is incorrect statement about find and findOne operations in MongoD…
