【问题标题】:What the difference between insert, update and save?插入、更新和保存有什么区别?
【发布时间】:2020-12-19 13:27:22
【问题描述】:

这些方法有什么区别 .insert_many(), .save(), .update(), .update_many() ?


我想将文档推送到数据库,如果存在相同的文档,它将被更新,如果不存在,它将被创建

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我尝试了 .update(upsert=True),但 python 告诉我这种方法已被弃用,以及哪种方法更适合我的任务的问题
  • 贴出你试过的代码
  • 我不使用 python,但快速的谷歌建议你看看替换 api.mongodb.com/python/current/api/pymongo/collection.html
  • @deadshot 我只使用“collection.update(file, upsert=true)”代码

标签: python mongodb pymongo pymongo-3.x


【解决方案1】:

From docs

更新(规范,文档,upsert=False,操作=False,multi=False,check_keys=True,**kwargs) 更新此集合中的文档。

已弃用 - 改用 replace_one()、update_one() 或 update_many()。

使用update_one()的示例查询

collection.update_one({'key': 'value'}, {'$set': {'new_key': 'new_value'}}, upsert=True)

使用replace_one()

collection.update_one({'key': 'value'}, {'new_key': 'new_value'}, upsert=True)

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多