Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 32
You are working with a products collection. Each document has the following structure:
- {
- "_id": ObjectId("..."),
- "name": "Laptop",
- "price": 999.99,
- "tags": ["electronics", "portable", "sale"],
- "available": true
- }
Which query correctly filters documents where:
The product is available,
The
priceis greater than or equal to 500,The
tagsarray includes the value"sale"?
-
A
- db.products.find({
- available: true,
- price: { $gte: 500 },
- tags: { $in: ["sale"] }
- })
-
B
- db.products.find({
- $and: [
- { price: { $gte: 500 } },
- { tags: { $eq: "sale" } },
- { available: true }
- ]
- })
-
C
- db.products.find({
- price: { $gte: 500 },
- tags: "sale",
- available: true
- })
-
D
- db.products.find({
- available: true,
- price: { $gte: 500 },
- tags: { $all: ["sale"] }
- })
Reveal correct answer
Correct answer: A
A.
This is the correct and idiomatic way to filter:
$gtechecks the price condition.{ tags: { $in: ["sale"] } }checks if the array contains"sale".available: truechecks availability.
B.
{ tags: { $eq: "sale" } } is redundant and less idiomatic in MongoDB, as $eq is implicit. Also, $eq won't match a value inside an array, which causes this filter to fail.
C.
While this seems correct at first glance, it relies on MongoDB's behavior that matches arrays directly. Although this would work in many cases, it’s not explicit. $in is a better, more readable and predictable option.
D.
$all: ["sale"] would still match, but it is intended for matching multiple elements. It's valid, but unnecessary for matching a single value — $in is preferred and more efficient.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
