【问题标题】:MongoDB with python - two actions in one queryMongoDB 与 python - 一个查询中的两个操作
【发布时间】:2013-11-15 13:51:52
【问题描述】:

我尝试在一个查询中使用 2 个操作来更改文档:

_userstats.update(
    {"nick" : nick},
    {"$set" : {"online" : True}},
    {"$inc" : {"joined" : 1}})

但是当我尝试这个时,我得到了错误:

raise TypeError("upsert must be an instance of bool")
TypeError: upsert must be an instance of bool

我没有得到这个工作。 谁能帮我弄清楚我到底错在哪里?

【问题讨论】:

标签: python mongodb


【解决方案1】:

您应该将所有更新操作放在一个字典中,作为第二个参数传递给update

_userstats.update(
    {"nick" : nick},
    {"$set" : {"online" : True}, "$inc" : {"joined" : 1}})

【讨论】:

    【解决方案2】:

    您将第二个操作作为第三个参数传递,这不是 update 函数的工作方式。您应该将所有操作放在一个对象中。试试这个:

    _userstats.update(
        {"nick" : nick},
        {{"$set" : {"online" : True}},
         {"$inc" : {"joined" : 1}}})
    

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 1970-01-01
      • 2021-02-06
      • 2021-09-28
      • 2015-12-15
      • 2021-11-14
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      相关资源
      最近更新 更多