81

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)
4
  • 1
    Duplicate of stackoverflow.com/questions/4372797/…
    – jdotjdot
    Commented Dec 4, 2012 at 19:56
  • Does the same thing apply for update of an existing field?
    – gizgok
    Commented Dec 4, 2012 at 19:58
  • Pretty sure it does, yes. Try it and see what happens.
    – jdotjdot
    Commented Dec 4, 2012 at 20:05
  • 1
    For one you should be doing "for k,v in p['d'].iteritems()" - also, Mark's answer below does work without creating a new key/value pair. You should post more code so we can see what you are doing wrong. Commented Dec 4, 2012 at 23:13

4 Answers 4

138

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.

3
  • This introduces a new key-value pair
    – gizgok
    Commented Dec 4, 2012 at 20:30
  • 1
    There are missing } in both update statements at the $set and $inc part.
    – Eleni
    Commented Jun 4, 2013 at 9:52
  • 1
    $set should be quoted: '$set'. Otherwise it throws syntax error. Commented Sep 18, 2013 at 11:16
9

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)
2
  • faced with TypeError Exception Value: Object of type 'ObjectId' is not JSON serializable on django1.11
    – Yuseferi
    Commented Nov 10, 2017 at 19:58
  • This looks kind of awesome, but I don't think it works. in the '$set', existing + 1 is not a lambda, so it will use whatver existing variable is in scope (or raise an exception if there is none), rather than using the actual existing value in the field.
    – Rasmus Kaj
    Commented Sep 29, 2023 at 11:28
2

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]}})
1
  • 1
    DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
    – alper
    Commented Oct 11, 2021 at 13:50
0

In Python, the operators (e.g., $set) should be in quotes:

db.ProductData.update(
    {'fromAddress': 'http://localhost:7000/'},
    {'$set': {'fromAddress': 'http://localhost:5000/'}},
    {'multi': True}
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.