我想用pymongo(3.0.3版)根据文档的ID更新现有的mongodb文档。但是,我的python代码没有任何更新。有人知道为什么吗..?
下面是我想要运行的mongodb脚本,它在mongo客户端Robomongo上运行得很好。
db.mycollection.update(
{ '_id': ObjectId("55d49338b9796c337c894df3") },
{ $set: { "details.model": "14Q22" } },
upsert=false
)但是,这段python代码不起作用。
client = pymongo.MongoClient("1.1.1.1", 27017)
db = client.mydb
db.mycollection.update(
{ '_id': "55d49338b9796c337c894df3" },
{ '$set': { "details.model": "14Q22" } },
upsert=False
)它返回
{u'n': 0, u'nModified': 0, u'ok': 1, 'updatedExisting': False}发布于 2015-08-25 00:35:17
可以,您必须指定一个ObjectId实例,如下所示:
from bson.objectid import ObjectId
db.mycollection.update({'_id': ObjectId("55d49338b9796c337c894df3")}, {'$set': {"details.model": "14Q22"}}) mycollection.update可以工作,但由于引入了新的CRUD操作而被弃用,因此最好使用mycollection.update_one或CRUDhttps://stackoverflow.com/questions/32186503
复制相似问题