Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 33
You have a MongoDB collection named employees that contains documents with fields name, department, and salary. You want to update the salary of all employees in the "Marketing" department to $75,000. The original collection looks like this:
- [
- { "_id": 1, "name": "Alice", "department": "Marketing", "salary": 60000 },
- { "_id": 2, "name": "Bob", "department": "Sales", "salary": 55000 },
- { "_id": 3, "name": "Charlie", "department": "Marketing", "salary": 62000 },
- { "_id": 4, "name": "David", "department": "Engineering", "salary": 70000 }
- ]
You run the following MongoDB update operation:
- db.employees.updateMany(
- { "department": "Marketing" },
- { $set: { "salary": 75000 } }
- )
What will the collection look like after this update operation?
-
A
- [
- { "_id": 1, "name": "Alice", "department": "Marketing", "salary": 60000 },
- { "_id": 2, "name": "Bob", "department": "Sales", "salary": 75000 },
- { "_id": 3, "name": "Charlie", "department": "Marketing", "salary": 75000 },
- { "_id": 4, "name": "David", "department": "Engineering", "salary": 70000 }
- ]
-
B
- [
- { "_id": 1, "name": "Alice", "department": "Marketing", "salary": 75000 },
- { "_id": 2, "name": "Bob", "department": "Sales", "salary": 55000 },
- { "_id": 3, "name": "Charlie", "department": "Marketing", "salary": 75000 },
- { "_id": 4, "name": "David", "department": "Engineering", "salary": 70000 }
- ]
-
C
- [
- { "_id": 1, "name": "Alice", "department": "Marketing", "salary": 62000 },
- { "_id": 2, "name": "Bob", "department": "Sales", "salary": 55000 },
- { "_id": 3, "name": "Charlie", "department": "Marketing", "salary": 62000 },
- { "_id": 4, "name": "David", "department": "Engineering", "salary": 70000 }
- ]
-
D
- [
- { "_id": 1, "name": "Alice", "department": "Marketing", "salary": 60000 },
- { "_id": 2, "name": "Bob", "department": "Sales", "salary": 55000 },
- { "_id": 3, "name": "Charlie", "department": "Marketing", "salary": 75000 },
- { "_id": 4, "name": "David", "department": "Engineering", "salary": 70000 }
- ]
Reveal correct answer
Correct answer: B
A.
This option incorrectly updates Bob's salary instead of Alice's. The update operation specifically targets documents with the "Marketing" department, not the "Sales" department.
B.
This option correctly shows that the salary of both Alice and Charlie, who belong to the "Marketing" department, has been updated to $75,000. The updateMany method applies the $set operation to all documents that match the filter { "department": "Marketing" }, which includes both Alice and Charlie.
C.
This option does not apply the update correctly; it should set the salary to $75,000 for all "Marketing" employees, but it leaves the salaries unchanged, which indicates a misunderstanding of how $set works in combination with updateMany.
D.
This option only updates Charlie's salary. However, the updateMany command targets all documents matching the condition, not just the last or a specific one. Therefore, Alice's salary should have been updated as well.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
