Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo update multiple records with multiple data

I am trying to store data the data of my dictionary in my database via PyMongo.

client = MongoClient('ip', port)
db = client.test_database
hdd = db.hdd

        products[{
        'Speed' : 'a', 
        'Capacity' : 'b',
        'Format' : 'c'
        }
        {
        'Speed' : 'd',
        'Capacity' : 'e', 
        'Format': 'f'}] ...

My database has a table hdd with 7 fields and 4 of them are already filled. The values of Speed, capacity and format are "" and need to be replaced with the data of products. I want to fill the empty fields with the data of the dictionary. Is there a way to update hdd like that, and if it's possible, how?

like image 942
henktenk Avatar asked Dec 08 '14 14:12

henktenk


People also ask

How do you update multiple documents with different values?

To update multiple documents in a collection, set the multi option to true. multi is optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.

How do I update multiple entries in MongoDB?

Update Multiple Fields of a Single Document. We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.

What is the difference between update and Upsert in MongoDB?

Or in other words, upsert is a combination of update and insert (update + insert = upsert). If the value of this option is set to true and the document or documents found that match the specified query, then the update operation will update the matched document or documents.


2 Answers

initialize_ordered_bulk_op shows as deprecated in my PyCharm (I have pymongo 3.9). Bulk updates can be done as follows:

replacements = [{ ObjectId("5fa994e96bfcb746d4935778"): "new_value"}]
bulk_ops = []
for _id, new_value in replacements.items():
    bulk_ops.append(
        UpdateOne(
            {"_id": _id},
            {"$set": {"old_key": new_value, "other_key": other_value}},
        ) 
    )
result = db.coll.bulk_write(bulk_ops)
pprint(result.bulk_api_result)
like image 65
Noumenon Avatar answered Oct 21 '22 17:10

Noumenon


I assume you have some sort of "_id" value associated with each set of values, so you know which document in your collection to update? Let's call that "product_id". You can update individual documents like:

for product, product_id in data:
    hdd.update({'_id': product_id},
               {'$set': {'Speed': products['Speed'],
                         'capacity': products['capacity'],
                         'format': products['format']}})

The first argument to update is a query that specifies which document to match, the second is a set of update operations.

If you're on MongoDB 2.6 or later and the latest PyMongo, use a bulk update:

bulk = hdd.initialize_ordered_bulk_op()
for product, product_id in data:
    bulk.find({'_id': product_id}).update({'$set': {'Speed': products['Speed'],
                                                   'capacity': products['capacity'],
                                                   'format': products['format']}})
bulk.execute()

The operations are buffered on the client, then they're all sent to the server and executed at once when you call "execute()". Bulk update operations with PyMongo and MongoDB 2.6+ require fewer round trips to the server than traditional updates.

like image 20
A. Jesse Jiryu Davis Avatar answered Oct 21 '22 19:10

A. Jesse Jiryu Davis