I have a problem with mongo with a list of data.

I have some items in Mongo DB and I need to update some of them with different data.

Example,

in DB now I have:

{"data": [{"_id": ObjectId("60e422a7c37c8d5c7f92add9"),"date": "2021-07-06 09:30:12.193452","from_cache": false,"message_hash": "8ee640883e8bce3ebed1dba8a0c6c7e5","predict_result": "Spam","probability": "0.5316912313644865","processing_duration": 378.8},{"_id": ObjectId("60e422b1c37c8d5c7f92addb"),"date": "2021-07-06 09:30:24.465770","from_cache": false,"message_hash": "b0384a99d40dfe6375d615689e02dfb2","predict_result": "Spam","probability": "0.7002104","processing_duration": 49.75},{"_id": ObjectId("60e422b1c37c8d5c7f92addc"),"date": "2021-07-06 09:30:17.535144","from_cache": false,"message_hash": "6d7b2640368d0b98dc81a3cde41b7ed9","predict_result": "Spam","probability": "0.6056733","processing_duration": 35.61},{"_id": ObjectId("60e422c5c37c8d5c7f92addf"),"date": "2021-07-06 09:30:42.515050","from_cache": false,"message_hash": "910be0727279462f6cca4d5b43093387","predict_result": "Spam","probability": "0.8142542043496809","processing_duration": 17.57},{"_id": ObjectId("60e423b5c37c8d5c7f92adf8"),"date": "2021-07-06 09:34:38.985356","from_cache": true,"message_hash": "910be0727279462f6cca4d5b43093387","predict_result": "Spam","probability": "0.8142542043496809","processing_duration": 1.08},{"_id": ObjectId("60e42f6ec37c8d5c7f92af25"),"date": "2021-07-06 10:24:40.329178","from_cache": false,"message_hash": "ad2f8a932272db08debaff29cb8feb69","predict_result": "Spam","probability": "0.7841739770961472","processing_duration": 14.71},{"_id": ObjectId("60e42f78c37c8d5c7f92af27"),"date": "2021-07-06 10:24:48.530278","from_cache": false,"message_hash": "d8d6cbc88b02d4dc4adb465d76843c00","predict_result": "Spam","probability": "0.7841739770961472","processing_duration": 5.93}]}

I get requests from the end-point and need to update "predict_result" some of them with different data.

I try to do so:

def update_many(self, ids: list, data):query = {'_id': {"$in": ids}}newvalues = {"$set":{data}}self.collection.update_many(query, newvalues)

But it doesn't work.

Thnx a lot for helping!

1

Best Answer


Assuming that 'data' is a python dict, you should use this:

def update_many(self, ids: list, data):query = {"_id": {"$in": ids}}newvalues = {"$set": data}self.collection.update_many(query, newvalues)

This would update the fields in 'data'.