users with documents containing a name field, which query returns all users whose names start with the letter 'A'?db.users.find({ name: { $regex: '^A' } })The caret ^ in regex means the start of the string. So ^A matches names starting with 'A'. Option A matches names ending with 'A'. Option A matches names containing 'A' anywhere. Option A matches names starting with lowercase 'a', which is case sensitive.
contacts collection where the email field ends with '.com'?db.contacts.find({ email: { $regex: '\.com$' } })The dollar sign $ matches the end of the string. The dot . is a special character in regex, so it must be escaped with a backslash \. to match a literal dot. Option D correctly matches emails ending with '.com'. Option D matches strings starting with '.com'. Option D matches any string containing 'com' with any character before it. Option D matches any string containing '.com' anywhere.
Option B is missing the closing brace } for the find method call, causing a syntax error. The other options have correct syntax.
employees where the department field contains the word 'sales' regardless of case. Which query is the most efficient and correct?Option C uses the $options: 'i' flag to make the regex case-insensitive, which is efficient and clear. Option C tries to use inline case-insensitive flag which MongoDB does not support. Option C manually matches each letter case which is less efficient. Option C is case-sensitive and misses matches with different cases.
db.logs.find({ message: { $regex: 'error' } }). Which statement best describes the behavior of this query?Without anchors like ^ or $, the regex matches the pattern anywhere in the string. So option A is correct. Options A and B require anchors. Option A requires exact match, which regex alone does not guarantee.