MongoDB MCQs – Querying Data

11.) Which operator is used to query embedded documents in MongoDB?

A) $elemMatch
B) $nested
C) $lookup
D) Dot notation

Answer: Option D

Explanation: Dot notation is used to query and project fields inside embedded documents.

12.) How do you limit the number of documents returned in a query?

A) db.collection.find().limit(n)
B) db.collection.limit(n)
C) db.collection.query.limit(n)
D) db.collection.resultLimit(n)

Answer: Option A

Explanation: The limit(n) method restricts the number of documents returned in a query to n.

13.) How do you sort query results in MongoDB?

A) db.collection.sort({ field: 1 })
B) db.collection.find().sort({ field: 1 })
C) db.collection.find({}).order({ field: 1 })
D) db.collection.orderBy({ field: 1 })

Answer: Option B

Explanation: The sort() method orders the query results. { field: 1 } sorts in ascending order, while { field: -1 } sorts in descending order.

14.) Which operator matches documents where all array elements meet the specified criteria?

A) $size
B) $match
C) $all
D) $elemMatch

Answer: Option D

Explanation: The $elemMatch operator matches documents where at least one array element satisfies all specified criteria.

15.) How do you include only specific fields in a query result?

A) { field: 1 }
B) { field: 0 }
C) { field: include }
D) { field: true }

Answer: Option A

Explanation: Using { field: 1 } includes the specified field in the query result.

16.) Which operator is used to match all values in an array?

A) $all
B) $in
C) $exists
D) $and

Answer: Option A

Explanation: The $all operator matches documents where a field’s value contains all the specified elements.

17.) What does the $regex operator do?

A) Matches values equal to a string
B) Matches values based on a regular expression
C) Checks if a field exists
D) Matches values not in an array

Answer: Option B

Explanation: The $regex operator is used for pattern matching in strings using regular expressions.

18.) Which MongoDB method skips a specific number of documents in query results?

A) db.collection.skip()
B) db.collection.query.skip(n)
C) db.collection.find().skip(n)
D) db.collection.limit().skip(n)

Answer: Option C

Explanation: The skip(n) method skips the first n documents in the query result.

19.) What does the $type operator check?

A) If a field exists
B) The data type of a field
C) The value of a field
D) The size of an array

Answer: Option B

Explanation: The $type operator matches documents where a field is of a specific BSON data type.

20.) Which MongoDB query method limits the output to specified fields only?

A) db.collection.find({}, { fields })
B) db.collection.find({}, { field: 1 })
C) db.collection.query({ fields: 1 })
D) db.collection.select({ field })

Answer: Option B

Explanation: Using { field: 1 } in the find() method specifies that only the selected fields should be returned in the query output.

Leave a Reply

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