Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update values using pymongo?

I've a mongodb collection in this form:

{id=ObjectId(....),key={dictionary of values}}
where dictionary of values is {'a':'1','b':'2'.....}

Let dictionary of values be 'd'. I need to update the values of the key in the 'd'. i.e I want to change 'a':'1' to 'a':'2' How can do I this in pymongo?

Code goes something like this:

productData is a collection in mongoDB
for p in productData.find():
     for k,v in p.iteritems():
         value=v['a']
         value=value+1
         v['a']=value

Now reflect the new value in the productData.

This is what I've tried and it introduces a new key-value pair instead of updating the

for p in productData.find():
    for k,v in p.iteritems():
         value=v['a']
         value=value+1
         v['a']=value
         productData.update({'_id':mongoId},{"$set":{'d.a':'100'}},upsert=False)
like image 596
gizgok Avatar asked Dec 04 '12 19:12

gizgok


People also ask

How do you update a value in PyMongo?

You can update a record, or document as it is called in MongoDB, by using the update_one() method. The first parameter of the update_one() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated.

How do I update all documents in PyMongo?

Updating all Documents in a Collection. PyMongo includes an update_many() function which updates all the documents which satisfy the given query. filter – It is the first parameter which is a criteria according to which the documents that satisfy the query are updated.

How do you update a collection in Python?

We can update data in a collection using update_one() method and update_many() method.


3 Answers

You can use the $set syntax if you want to set the value of a document to an arbitrary value. This will either update the value if the attribute already exists on the document or create it if it doesn't. If you need to set a single value in a dictionary like you describe, you can use the dot notation to access child values.

If p is the object retrieved:

existing = p['d']['a']

For pymongo versions < 3.0

db.ProductData.update({
  '_id': p['_id']
},{
  '$set': {
    'd.a': existing + 1
  }
}, upsert=False, multi=False)

For pymongo versions >= 3.0

db.ProductData.update_one({
  '_id': p['_id']
},{
  '$set': {
    'd.a': existing + 1
  }
}, upsert=False)

However if you just need to increment the value, this approach could introduce issues when multiple requests could be running concurrently. Instead you should use the $inc syntax:

For pymongo versions < 3.0:

db.ProductData.update({
  '_id': p['_id']
},{
  '$inc': {
    'd.a': 1
  }
}, upsert=False, multi=False)

For pymongo versions >= 3.0:

db.ProductData.update_one({
  '_id': p['_id']
},{
  '$inc': {
    'd.a': 1
  }
}, upsert=False)

This ensures your increments will always happen.

like image 81
Mark Unsworth Avatar answered Oct 21 '22 11:10

Mark Unsworth


With my pymongo version: 3.2.2 I had do the following

from bson.objectid import ObjectId
import pymongo

client = pymongo.MongoClient("localhost", 27017)
db = client.mydbname

db.ProductData.update_one({
  '_id': ObjectId(p['_id']['$oid'])
},{
  '$set': {
    'd.a': existing + 1
  }
}, upsert=False)
like image 10
Raptor Avatar answered Oct 21 '22 12:10

Raptor


Something I did recently, hope it helps. I have a list of dictionaries and wanted to add a value to some existing documents.

for item in my_list:
    my_collection.update({"_id" : item[key] }, {"$set" : {"New_col_name" :item[value]}})
like image 2
Jack Avatar answered Oct 21 '22 11:10

Jack