I'm looking to update all documents in a database for users such that each user gets an update similar to
for user in user_to_state:
collection_users.update_one({"username":user}, {$set: {"state":user_to_state[user]}})
But I'd like to do it in a single query rather than iterating through the dictionary updating each one at a time. Is this possible? Is it faster than iterating?
If I have a dictionary of each user's username (a field currently on every document in this collection) and their state e.g. {"jim12":"TX", "jane34":"FL", ...} is there a way to do a singular query that submits the entire dictionary and goes through each or does it have to be iterated through with the update_one method?
Thanks in advance!
source https://stackoverflow.com/questions/74817313/how-to-update-mongodb-pymongo-according-to-dictionary
Comments
Post a Comment