Updating multiple documents with specific filters using pymongo

john

I would like to update multiple documents in a mongoDB database using pymongo. I have this data:

data_to_be_updated = [
    {"sourceID" : 6, "source" : "test", "name" : "simon"},
    {"sourceID" : 8, "source" : "test", "name" : "greg"},
    {"sourceID" : 9, "source" : "test", "name" : "julie"},
    {"sourceID" : 10, "source" : "test", "name" : "john"}
    ]
sourceIDs = [6, 8, 9, 10]

I would like to update each of the elements in data_to_be_inserted, filtering them by their sourceID. I have tried using the update_many function, but it updates all documents matching a single filter. I could of course use a for loop like this:

for item in data_to_be_updated:
    collection.update_one({"sourceID": item["sourceID"]}, item})

The above method uses to many calls. How do i achieve the same in a single call to the database?

A. Jesse Jiryu Davis

Use bulk_write. Something like this, depending on exactly what fields need to be updated:

from pymongo.operations import UpdateOne

data_to_be_updated = [
    {"sourceID": 6, "source": "test", "name": "simon"},
    {"sourceID": 8, "source": "test", "name": "greg"},
    {"sourceID": 9, "source": "test", "name": "julie"},
    {"sourceID": 10, "source": "test", "name": "john"}
]

result = collection.bulk_write([
    UpdateOne(filter={'sourceID': d['sourceID']},
              update={'$set': {'name': d['name'],
                               'source': d['source']}})
    for d in data_to_be_updated])

print(result.bulk_api_result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Updating data (adding new fields) to MongoDB documents using a csv file (pymongo)

From Dev

MongoDb pymongo how to increment multiple documents

From Dev

Updating multiple documents with different values

From Dev

Lucene filtering indexed documents with multiple filters

From Dev

How to add documents to an array in a collection using PyMongo

From Dev

Using aliases with multiple filters

From Dev

Using Solr DataImportHandler for updating documents

From Dev

Checking for existence of _id using and updating subdocument Pymongo

From Dev

Updating multiple documents in elasticsearch using Java. I am using Elasticsearch 2.3.2

From Dev

Updating multiple sub-documents via Mongoose?

From Dev

updating Mongoose documents with nested arrays at multiple indexes

From Dev

Elastic - updating multiple documents in a single request

From Dev

Updating multiple documents on nested object change

From Dev

Using $unwind on multiple documents

From Dev

Angular JS Using "Multiple Filters"

From Dev

Bigcommerce using multiple filters with cURL

From Dev

Updating multiple imageview for specific value

From Dev

Updating documents of mongodb using react nodejs and ajax

From Dev

Remove attribute from all MongoDB documents using Python and PyMongo

From Dev

Best way to read and update mongodb documents using pymongo

From Dev

Remove attribute from all MongoDB documents using Python and PyMongo

From Dev

Updating a value of dictionary in list of dictionaries in a collection using PyMongo

From Dev

Updating a value of dictionary in list of dictionaries in a collection using PyMongo

From Dev

How to update multiple values in Mongodb using pymongo?

From Dev

Bulk update in Pymongo using multiple ObjectId

From Dev

How to update multiple values in Mongodb using pymongo?

From Dev

Pymongo Return documents count

From Dev

AngularJS - ng-repeat not updating to correct results using filters

From Dev

Thinking Sphinx - multiple conditions for attribute filters using OR

Related Related

  1. 1

    Updating data (adding new fields) to MongoDB documents using a csv file (pymongo)

  2. 2

    MongoDb pymongo how to increment multiple documents

  3. 3

    Updating multiple documents with different values

  4. 4

    Lucene filtering indexed documents with multiple filters

  5. 5

    How to add documents to an array in a collection using PyMongo

  6. 6

    Using aliases with multiple filters

  7. 7

    Using Solr DataImportHandler for updating documents

  8. 8

    Checking for existence of _id using and updating subdocument Pymongo

  9. 9

    Updating multiple documents in elasticsearch using Java. I am using Elasticsearch 2.3.2

  10. 10

    Updating multiple sub-documents via Mongoose?

  11. 11

    updating Mongoose documents with nested arrays at multiple indexes

  12. 12

    Elastic - updating multiple documents in a single request

  13. 13

    Updating multiple documents on nested object change

  14. 14

    Using $unwind on multiple documents

  15. 15

    Angular JS Using "Multiple Filters"

  16. 16

    Bigcommerce using multiple filters with cURL

  17. 17

    Updating multiple imageview for specific value

  18. 18

    Updating documents of mongodb using react nodejs and ajax

  19. 19

    Remove attribute from all MongoDB documents using Python and PyMongo

  20. 20

    Best way to read and update mongodb documents using pymongo

  21. 21

    Remove attribute from all MongoDB documents using Python and PyMongo

  22. 22

    Updating a value of dictionary in list of dictionaries in a collection using PyMongo

  23. 23

    Updating a value of dictionary in list of dictionaries in a collection using PyMongo

  24. 24

    How to update multiple values in Mongodb using pymongo?

  25. 25

    Bulk update in Pymongo using multiple ObjectId

  26. 26

    How to update multiple values in Mongodb using pymongo?

  27. 27

    Pymongo Return documents count

  28. 28

    AngularJS - ng-repeat not updating to correct results using filters

  29. 29

    Thinking Sphinx - multiple conditions for attribute filters using OR

HotTag

Archive