Reputation: 33
I tried to bind both the comparing attribute and $set attribute in a single variable and rendered to update function in mongodb. But it give me the below error
TypeError: update() missing 1 required positional argument: 'document'
Python 3 MongoDB
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client.Mydb
collection = db.sampledb
new_contact = "6369723748"
updatestmt = "{\"ID\" : \"12345\"},{\"$set\" :{\"ID\" : \"67891\",\"Account_Number\" : \"1234 5678 9101\"}}"
print(updatestmt)
cursor = collection.update(updatestmt)
cursor1 = collection.find()
for i in cursor1:
print(i)
Expected Result:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Actual Result:
Traceback (most recent call last):
File "dbupdate.py", line 12, in
cursor = collection.update(updatestmt)
TypeError: update() missing 1 required positional argument: 'document'
Upvotes: 3
Views: 17175
Reputation: 2166
update()
is deprecated and you should really use update_one() instead.
cursor = collection.update({'ID' : '12345'}, {'$set' : {'ID' : '67891', 'Account_Number' : '1234 5678 9101'}})
Upvotes: 2
Reputation: 9881
pymongo
is working with dictionaries, not strings. (the error just tells you you are passing one single string, while it is expecting at least two arguments).
Hence use:
query = {"ID" : "12345"}
new_values = {"$set" : {"ID" : "67891", "Account_Number" : "1234 5678 9101"}}
cursor = collection.update(query, new_values)
Or, to match your syntax:
updatestmt = ({"ID" : "12345"}, {"$set" : {"ID" : "67891", "Account_Number" : "1234 5678 9101"}})
cursor = collection.update(*updatestmt)
BTW: update
is now replaced with update_one
(for your need), or update_many
(if the query could match multiple records)
Upvotes: 2