[python] 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)

This question is related to python mongodb pymongo

The answer is


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)

in python the operators should be in quotes: db.ProductData.update({'fromAddress':'http://localhost:7000/'}, {"$set": {'fromAddress': 'http://localhost:5000/'}},{"multi": True})


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]}})

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?

Examples related to pymongo

How to convert a pymongo.cursor.Cursor into a dict? JSON ValueError: Expecting property name: line 1 column 2 (char 1) How to update values using pymongo? mongo - couldn't connect to server 127.0.0.1:27017 Query Mongodb on month, day, year... of a datetime How to sort mongodb with pymongo How do I update a Mongo document after inserting it?