mongodbflask-pymongo

Need equivalent Pymongo command for for the manual working command in MongoDB prompt using array filters


Need the Pymongo (Python Flask) equivalent command for the manual working command:

db.UserInfoCollection.update({ "id" : "6efb83dc365fb6bdb3b78a9a"},
                             {$set: {'testArrayLevel1.$[i].testArrayLevel2.$[j].status':"registered"}},
                             {arrayFilters:[{"i.user_name":"test_user1"}, {"j.user_type": "basic"}]})

The above command updated the db at manual Mongodb prompt without any issues.

Tried the following (both update and update_one and arrayFilters and array_filters):

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

This command gives error when executed in Python Flask code:

**TypeError: update_one() got multiple values for argument 'upsert'**
mongo.db.UserInfoCollection.update_one({'id': id}, {"$set": {'testArrayLevel1.$[i].testArrayLevel2.$[j].status':"registered"}},{"array_filters":[{"i.user_name":user_name}, {"j.user_type": user_type}]})

This command gives error when executed in Python Flask code: TypeError: upsert must be True or False

Something is missing in my command formation in Pymongo for the working command. Any help is appreciated here.


Solution

  • In pymongo you have to pass array_filters as a separate parameter; e.g.

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

    Reference: pymongo documentation