I want to update an existing mongodb document by its ID with pymongo(version 3.0.3). However nothing gets updated with my python code. Does anyone know why..?
Here is the mongodb script I want to run and this works fine from a mongo client, Robomongo.
db.mycollection.update(
{ '_id': ObjectId("55d49338b9796c337c894df3") },
{ $set: { "details.model": "14Q22" } },
upsert=false
)
However this python code does not work.
client = pymongo.MongoClient("1.1.1.1", 27017)
db = client.mydb
db.mycollection.update(
{ '_id': "55d49338b9796c337c894df3" },
{ '$set': { "details.model": "14Q22" } },
upsert=False
)
It returns
{u'n': 0, u'nModified': 0, u'ok': 1, 'updatedExisting': False}
Yes, you have to specify an ObjectId
instance as follows:
from bson.objectid import ObjectId
db.mycollection.update({'_id': ObjectId("55d49338b9796c337c894df3")}, {'$set': {"details.model": "14Q22"}})
mycollection.update
, although it works, is deprecated
due to introduction of new CRUD operations, better use
mycollection.update_one
or mycollection.find_one_and_update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With