Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 11
In the PyMongo driver for MongoDB, which of the following is the correct syntax for updating multiple documents in a collection that match a specific condition?
-
A
db.collection.update({'age': {'$gt': 21}}, {'$set': {'eligible': True}}, upsert=False, multi=True) -
B
db.collection.update_many('age' > 21, {'$set': {'eligible': True}}) -
C
db.collection.update_all({'age': {'$gt': 21}}, {'$set': {'eligible': True}}) -
D
db.collection.update_many({'age': {'$gt': 21}}, {'$set': {'eligible': True}})
Reveal correct answer
Correct answer: D
A.
In pymongo, the update method has been deprecated and replaced with update_one and update_many. Furthermore, upsert=False is redundant here as it is the default setting.
B.
This would cause a syntax error because the filter criteria needs to be a dictionary, not a string comparison.
C.
There is no update_all method in pymongo. The correct method for updating multiple documents is update_many.
D.
This is the correct syntax to update multiple documents in MongoDB using pymongo. It selects the documents where the age is greater than 21 and updates the 'eligible' field to True.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
