Python 如何使用 pymongo 更新值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13710770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:26:22  来源:igfitidea点击:

How to update values using pymongo?

pythonmongodbpymongo

提问by gizgok

I've a mongodb collection in this form:

我有一个这种形式的 mongodb 集合:

{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?

设值字典为'd'. 我需要更新'd'. 即我想更改'a':'1''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.

现在在 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)

采纳答案by Mark Unsworth

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.

如果要将文档的值设置为任意值,可以使用 $set 语法。如果该属性已存在于文档中,则这将更新该值,否则将创建它。如果您需要像您描述的那样在字典中设置单个值,您可以使用点表示法来访问子值。

If p is the object retrieved:

如果 p 是检索到的对象:

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

For pymongo versions < 3.0

对于 pymongo 版本 < 3.0

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

For pymongo versions >= 3.0

对于 pymongo 版本 >= 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:

但是,如果您只需要增加该值,当多个请求可以同时运行时,这种方法可能会引入问题。相反,您应该使用 $inc 语法:

For pymongo versions < 3.0:

对于 pymongo 版本 < 3.0:

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

For pymongo versions >= 3.0:

对于 pymongo 版本 >= 3.0:

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

This ensures your increments will always happen.

这可确保您的增量始终发生。

回答by Raptor

With my pymongo version: 3.2.2 I had do the following

使用我的 pymongo 版本:3.2.2 我做了以下事情

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)

回答by Adarsh R

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

在 python 中,运算符应该用引号引起来: db.ProductData.update({'fromAddress':' http://localhost:7000/'}, {"$set": {'fromAddress': ' http://localhost: 5000/'}},{"multi": True})

回答by Hyman

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