Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 14
Given the following example document from an artists collection:
- {
- _id: 5,
- last_name: 'Maurer',
- first_name: 'Alfred',
- year_born: 1868,
- year_died: 1932,
- nationality: 'USA'
- }
And the following index:
- db.artists.createIndex( { "last_name": 1, "nationality": 1 } )
How MongoDB will handle the query below?
- db.artists.find( { "last_name": /^O./ } )
-
A
MongoDB will need to perform a full collection scan.
-
B
This query is incorrect.
-
C
As an index scan.
-
D
MongoDB will need to use the
$regexoperator for the query.
Reveal correct answer
Correct answer: C
A.
MongoDB does not need to perform a collection scan because there is an index on the "last_name" field, which allows for more efficient searching.
B.
The query is valid, and it searches for documents in the "artists" collection where the "last_name" field matches a pattern starting with the letter "O".
C.
The query is searching for documents in the "artists" collection where the "last_name" field matches a pattern that starts with the letter "O". Since an index exists on the "last_name" field, MongoDB can utilize the index to efficiently search for documents that match the query pattern. It will perform an index scan to find and return the matching documents.
D.
The $regex operator is typically used when you want to perform more complex pattern matching using regular expressions. It allows you to specify a regular expression as a string and apply it to a specific field in the query. However, in the given query, the regular expression pattern is directly specified in the query object without the need for the $regex operator.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
