【问题标题】:TypeError: update() got multiple values for argument 'upsert'TypeError:update() 为参数“upsert”获取了多个值
【发布时间】:2015-09-16 03:13:55
【问题描述】:

我正在编写一些 Python 代码来更新比赛的最后回填日期。我可以在 mongo shell 中手动执行此操作,但是当我尝试将查询转换为 pymongo 语法时,出现了问题。

代码:

    ShoutletContestConfig.objects().update(
        { '_id': contest },
        { '$set': { 'config.submission.twitter_retweet_last_backfilled': backfill_date } },
        **{ 'upsert': False }
    )

ShoutletContestConfig 是一个 mongo Document 子类。)

错误:

TypeError: update() got multiple values for argument 'upsert'

尝试缓解:

  • 省略 upsert 参数(嘿,mongo shell 足够聪明,可以选择默认值)。当我在 pymongo 中执行此操作时,它抱怨查询缺少 upsert 参数。
  • 字面输入upsert=False 而不是dict splat,以防万一。不,同样的错误。
  • pymongo API 文档对update() 的正确语法不明确;他们只是说“使用 Django 语法”,非常无益。
  • 将 pymongo 更新到 2.8 / 2.9。同样的错误。不幸的是,3.0 与我们的代码库不兼容。
  • 将 mongoengine 更新到 0.9 / 0.10。同样的错误。

系统:

  • pymongo 2.7
  • mongoengine 0.8.8
  • python 3.3.6
  • mongo 2.4.9
  • Mac OS X 10.10.5

【问题讨论】:

  • 您是否尝试过只传递 False 作为第三个参数,而不是 upsert=False
  • 是的,我试过了。同样的错误。

标签: python django mongodb pymongo


【解决方案1】:

事实证明需要双下划线的 Django 样式语法。

    ShoutletContestConfig.objects().filter(id=contest).update(
        set__config__submission__twitter_retweet_last_backfilled=backfill_date
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多