Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 31
You are working on a MongoDB collection called orders that stores customer orders. Each document has the following structure:
- {
- "_id": ObjectId("..."),
- "customerName": "John Doe",
- "orderTotal": 150,
- "orderStatus": "Processing",
- "items": [
- { "itemName": "Laptop", "quantity": 1 },
- { "itemName": "Mouse", "quantity": 2 }
- ]
- }
You need to update all orders with the status Processing by increasing the orderTotal by 10% if the orderTotal is greater than 100. Additionally, you must change the orderStatus to Ready to Ship. Which of the following update expressions correctly updates the documents according to the scenario?
-
A
- db.orders.updateMany(
- { "orderStatus": "Processing", "orderTotal": { "$gt": 100 } },
- { "$mul": { "orderTotal": 1.1 } }
- )
-
B
- db.orders.updateMany(
- { "orderTotal": { "$gt": 100 } },
- { "$set": { "orderStatus": "Ready to Ship" }, "$mul": { "orderTotal": 1.1 } }
- )
-
C
- db.orders.updateMany(
- { "orderStatus": "Processing", "orderTotal": { "$gt": 100 } },
- { "$set": { "orderStatus": "Ready to Ship" }, "$mul": { "orderTotal": 1.1 } }
- )
-
D
- db.orders.updateMany(
- { "orderStatus": "Processing" },
- { "$set": { "orderStatus": "Ready to Ship" }, "$inc": { "orderTotal": 10 } }
- )
Reveal correct answer
Correct answer: C
A.
This query correctly filters for documents where orderStatus is Processing and orderTotal is greater than 100. However, it only increases the orderTotal by 10% and does not update the orderStatus to Ready to Ship, missing one key requirement of the scenario.
B.
This query will update orders where the orderTotal is greater than 100 and will correctly multiply the orderTotal by 1.1. However, it fails to check if the orderStatus is Processing, which means it could erroneously update orders with different statuses.
C.
This query correctly selects documents where orderStatus is Processing and orderTotal is greater than 100. It then sets the orderStatus to Ready to Ship and multiplies the orderTotal by 1.1 (increasing it by 10%). Both conditions in the scenario are met with this update expression.
D.
This query sets the orderStatus to Ready to Ship for all orders with a Processing status, but it only increments the orderTotal by 10, not by 10%. Additionally, it does not check if the orderTotal is greater than 100, meaning it could incorrectly modify orders that do not meet the condition.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
