MongoDB MCQs – Querying Data

Querying data is one of the most crucial aspects of working with MongoDB. It involves retrieving, filtering, and projecting data stored in collections. Query operators such as $eq, $lt, $in, $and, and $not provide powerful ways to extract specific data, while projection allows fine-tuning of results by including or excluding fields. In interviews, you may be asked to solve complex query problems or optimize queries, making it essential to master these concepts.

1.) Which MongoDB operator matches documents where a field’s value equals the specified value?

A) $ne
B) $eq
C) $equals
D) $equal

Answer: Option B

Explanation: The $eq operator is used to match documents where a field’s value is equal to the specified value.

2.) Which of the following operators is used to match values less than a specified value?

A) $gte
B) $lessthan
C) $lte
D) $lt

Answer: Option D

Explanation: The $lt operator matches documents where the field’s value is less than the specified value.

3.) What does the $in operator do in MongoDB?

A) Matches any value from an array of specified values
B) Matches values not in the specified array
C) Checks for equality
D) Matches greater-than values

Answer: Option A

Explanation: The $in operator matches documents where a field’s value is within the specified array of values.

4.) How do you query documents where a field is greater than or equal to a value?

A) $gt
B) $gte
C) $gteq
D) $lte

Answer: Option B

Explanation: The $gte operator matches documents where the field’s value is greater than or equal to the specified value.

5.) Which operator is used to match multiple conditions that must all be true?

A) $and
B) $or
C) $not
D) $nor

Answer: Option A

Explanation: The $and operator is used to match documents that satisfy all the specified conditions.

6.) Which MongoDB operator negates the condition?

A) $ne
B) $nor
C) $not
D) $nin

Answer: Option C

Explanation: The $not operator negates the specified query condition.

7.) What is the function of the $or operator in MongoDB?

A) Matches documents where no condition is true
B) Matches documents with all conditions being true
C) Matches documents that satisfy one or more conditions
D) Matches documents with equality

Answer: Option C

Explanation: The $or operator matches documents that meet at least one of the specified conditions.

8.) How do you exclude a field in a query result using projection?

A) { field: 0 }
B) { field: 1 }
C) { field: -1 }
D) { field: false }

Answer: Option A

Explanation: In MongoDB, using { field: 0 } excludes the specified field from the query results.

9.) Which of the following operators checks whether a field exists in a document?

A) $check
B) $exists
C) $field
D) $include

Answer: Option B

Explanation: The $exists operator is used to check whether a specific field is present in a document.

10.) How do you query documents where a field value is not in a specified array?

A) $not
B) $nin
C) $ne
D) $not_in

Answer: Option B

Explanation: The $nin operator matches documents where a field’s value is not in the specified array of values.

Leave a Reply

Your email address will not be published. Required fields are marked *