0

I have a mongo database having data record like below

[
    {"name" : "a", "email" : "[email protected]","ml_pred":"valid","hum_pred":"null", "score":0.92},
    {"name" : "b","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null", "score":0.2},
    {"name" : "c","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "d","email" : "[email protected]","ml_pred":"null","hum_pred":"null"},
    {"name" : "e","email" : "[email protected]","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" : "[email protected]","ml_pred":"valid","hum_pred":"null","score":0.83},
    {"name" : "d","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null","score":0.12}
]
2
  • what is the creteria of adding those fields? add on specific name? specific _id? You need update and $set i think, see this example from docs Commented Apr 13, 2022 at 16:15
  • @Takis The criteria is based on specific email id. Email id will be unique for all. I am a new bie and just got stucked at this. Commented Apr 13, 2022 at 16:16

2 Answers 2

1

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": "[email protected]"}},
{"$set": {"ml_pred": "valid", "score": 0.83}})

Query2

Playmongo

update(
{"email": {"$eq": "[email protected]"}},
{"$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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the solution. How can it be done with bulk update or to do in a single step. I have several data to update like this at regular intervals. SO individual update will not be efficient. Can you show that how it is done too. Please upvote the question too. :)
Is it possible to do with aggregation pipeline method ?
i don't see a reason, to do aggregation pipeline update for example for 3 different mails, you would have to match one of the 3, and then check which one matched each time to make the update.If you had like 10 emails, you would have to write a switch case of 10. Bulk update i think is waht you need if you have many of those.
I have around 200 to 1000 varying number of update required at the same time. Thats the issue im facing
ok bulk update you need, put all those in a bulk, ordered or unordered and it will be fine,Just see how to do it in python. Try the above also its from the pymongo docs. Maybe someone that uses python can give the exact code, but i think it will be simple to do this in python. Bulk update means that you will send only 1 database command, with all those updates (and its faster).
0

I added multiple update and adding of fields by (based on the field email)

email_id=[data.pop("email") for data in datas]
operations=[UpdateOne({"email":email},{'$set':data}) for email ,data in zip(email_id,datas)]
mycol.bulk_write(operations)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.