MongoDB MongoDB Query Language and Operators 2 — Questions and Answers
Question 1: What does the $exists operator check in a MongoDB query?
- Whether a field value is null
- Whether a specified field is present in the document (Correct answer)
- Whether a document exists in the collection
- Whether an index exists on a field
Correct answer: Whether a specified field is present in the document
$exists matches documents that contain (or do not contain) the specified field, regardless of its value.
Question 2: Which MongoDB operator enables full-text search on fields that have a text index?
- $regex
- $search
- $like
- $text (Correct answer)
Correct answer: $text
$text performs a text search on the content of fields indexed with a text index.
Question 3: What does the $or operator return in a MongoDB query?
- Documents where all conditions are true
- Documents where at least one condition is true (Correct answer)
- Documents where exactly one condition is true
- Documents where none of the conditions are true
Correct answer: Documents where at least one condition is true
$or performs a logical OR, returning documents that satisfy at least one of the given conditions.
Question 4: Which MongoDB operator matches documents where a field value matches a given regular expression pattern?
- $like
- $match
- $regex (Correct answer)
- $pattern
Correct answer: $regex
$regex selects documents where field values match the specified regular expression.
Question 5: What does the $type operator do in a MongoDB query?
- Converts a field to a different BSON type
- Matches documents where a field is of a specified BSON type (Correct answer)
- Returns the type of a field as a string
- Creates a typed index on a field
Correct answer: Matches documents where a field is of a specified BSON type
$type selects documents where the value of a field matches the specified BSON type.
Question 6: What does the $nor operator do in a MongoDB query?
- Returns documents matching all specified conditions
- Returns documents matching at least one condition
- Returns documents that fail all of the specified conditions (Correct answer)
- Returns documents matching exactly one condition
Correct answer: Returns documents that fail all of the specified conditions
$nor returns documents that do not match any of the conditions in the array, the opposite of $or.
Question 7: Which operator is used to skip a specified number of documents in a MongoDB query result?
- .offset(n)
- .start(n)
- .from(n)
- .skip(n) (Correct answer)
Correct answer: .skip(n)
.skip(n) skips the first n documents in the result set, commonly used with .limit() for pagination.
What does the $exists operator check in a MongoDB query?