Mongodb Certified Developer Associate C100dev · Free Practice Question Easy
Question 26
You are working with a MongoDB collection named sales, where each document records a sales transaction. The documents contain fields such as transaction_id, customer_name, total_amount, and date. You need to retrieve all transactions where the total_amount is greater than $500. Which MongoDB query will correctly retrieve all transactions where the total_amount is greater than $500?
-
A
db.sales.find({ "total_amount": { $gte: 500 } }) -
B
db.sales.find({ "total_amount": 500 }) -
C
db.sales.find({ "total_amount": { $gt: "500" } }) -
D
db.sales.find({ "total_amount": { $gt: 500 } })
Reveal correct answer
Correct answer: D
A.
The $gte (greater than or equal to) operator will match documents where total_amount is either greater than or exactly equal to 500. While similar, it does not match the requirement of the scenario, which specifies retrieving amounts strictly greater than 500. This is a subtle but important distinction.
B.
This query only retrieves documents where total_amount is exactly 500. It does not use any relational operator, so it will not match any documents where total_amount is greater than 500. This reflects a misunderstanding of how to query for ranges in MongoDB.
C.
This query uses a string "500" instead of a numeric value 500. MongoDB treats different data types distinctly, so this query would not return the correct results unless total_amount is stored as a string in the database. This is a common mistake when users forget to match the data types correctly in queries.
D.
This query correctly uses the $gt (greater than) operator to retrieve documents where the total_amount field is greater than 500. The value 500 is treated as a numeric value, which is essential for accurate comparisons. This is the most straightforward and correct way to use a relational operator for this condition.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
