Search code examples
pythonmongodbpymongo

Updating and Adding on multiple documents using pymongo


I have a mongo database having data record like below

[
    {"name" : "a", "email" : "dsda@test.com","ml_pred":"valid","hum_pred":"null", "score":0.92},
    {"name" : "b","email" : "eda@test.com","ml_pred":"invalid","hum_pred":"null", "score":0.2},
    {"name" : "c","email" : "edfr@test.com","ml_pred":"null","hum_pred":"null"},
    {"name" : "d","email" : "edsd@test.com","ml_pred":"null","hum_pred":"null"},
    {"name" : "e","email" : "edxz@test.com","ml_pred":"null","hum_pred":"null"}
]

This data is inserted using insert_many in pymongo like

from pymongo import MongoClient
client = MongoClient('mongodb://testuser:testuser@mongo:27017/testdb?authSource=admin')
mydb = client["testdb"]    #Mongo database
mycol = mydb["todos"]    #Mongo Collection Name
mycol.insert_many(record)

How is it possible to do bulk update and add new fields in a single step to the 3rd and 4th document with the following data

[
    {"name" : "c","email" : "edfr@test.com","ml_pred":"valid","hum_pred":"null","score":0.83},
    {"name" : "d","email" : "edsd@test.com","ml_pred":"invalid","hum_pred":"null","score":0.12}
]

Solution

  • You can send 2 updates.You could also make them a bulk update, or you could also write a pipeline update to do both with 1 update, but i think you dont need more complicated things here.

    Both match the email, and then update the 2 fields.Using the update $set operator.

    Query1

    Playmongo

    update(
    {"email": {"$eq": "edfr@test.com"}},
    {"$set": {"ml_pred": "valid", "score": 0.83}})
    

    Query2

    Playmongo

    update(
    {"email": {"$eq": "edsd@test.com"}},
    {"$set": {"ml_pred": "invalid", "score": 0.12}})
    

    Edit

    I don't use pymongo i guess you need something like this but this depends on the python driver you use also, but it will be simple in all cases

    requests = [
        UpdateOne(...query1...),
        UpdateOne(...query2...)]
    try:
        db.test.bulk_write(requests, ordered=False)
    except BulkWriteError as bwe:
        pprint(bwe.details)