Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Smart way to update all documents in mongodb using python function

I execute some_python_function on all elements of collection.
This function returns different values for each document.

I developed the following function, but it’s very slow.

for doc in db.collection.find(query, projection):
    result = db.collection.update_one(
        {"_id": doc["_id"]}, 
        {"$set": {"field": some_python_function(doc["field"])}}
    )

I am looking any smarter way to do it, rather than updating documents one-by-one.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What would you recommend?

EDIT:
I have just found bulk operations in the API:
https://pymongo.readthedocs.io/en/stable/examples/bulk.html

updates = []
for doc in db.collection.find(filter, projection):
    if doc.get("titles"):
        updates.append(
            UpdateOne(
                {"_id": doc["_id"]}, {"$set": {"field": some_python_function(doc["field"]}}
            )
        )
result = collection.bulk_write(updates)

>Solution :

Use bulkWrite to write multiples document at once.

Here is an answer for a similar question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading