Search code examples
mongodbpymongo

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


testdb.update_one({'id': id}, {"$set": {'testArrayLevel1.$[i].testArrayLevel2.$[j].status':"registered"}},{"array_filters":[{"i.user_id":user_id}, {"j.user_type": user_type}]}, upsert=False)

Getting TypeError:

update() got multiple values for argument 'upsert'. If removed upsert=False, then getting:
TypeError: upsert must be True or False

What is the correct way to do this python code calling pymongo? Any inputs will be of great help.


Solution

  • The problem in the way you pass args to function, the value_args is malformed, so instead of

    testdb.update_one({'id': id}, {"$set": {'testArrayLevel1.$[i].testArrayLevel2.$[j].status':"registered"}},{"array_filters":[{"i.user_id":user_id}, {"j.user_type": user_type}]}, upsert=False)
    

    Use right passing for value_args, also formatting will make your life eathier:

    testdb.update_one({'id': id},
                      {
                          "$set": {'testArrayLevel1.$[i].testArrayLevel2.$[j].status':"registered"},
                          "array_filters":[{"i.user_id":user_id}, {"j.user_type": user_type}]
                      },
                      upsert=False)
    

    NOTE By default upsert is set to false, so may want to consider removing it, as I believe you used same call and the upsert were set to dict, which won't be the case after fixing arguments.