Python Pymongo 多更新查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23347095/
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-19 02:47:59  来源:igfitidea点击:

Pymongo multi update query

pythonmongodbpymongo

提问by user2950162

I have the following query

我有以下查询

DB_HOST = '127.0.0.1'
COLLECTION = 'scraper'
db = pymongo.MongoClient(DB_HOST)[COLLECTION]['scrap']
db.update({'indice':0, 'thread_id':{'$in':list_to_update}},{'updated':'yes'}, multi=True)

where list_to_update is a list of thread_ids where I would like to insert the field 'updated' to 'yes'

其中 list_to_update 是 thread_ids 列表,我想在其中插入字段“已更新”为“是”

I am receiving the following error

我收到以下错误

pymongo.errors.OperationFailure: multi update only works with $ operators

Any ideas?

有任何想法吗?

采纳答案by vaultah

Use the $setoperator:

使用$set运算符:

db.update({'indice':0, 'thread_id': {'$in': list_to_update}},
          {'$set': {'updated':'yes'}}, 
          multi=True)

回答by hamed

from pymongo 3.1 use update_many:

从 pymongo 3.1 使用update_many

xxx.update_many({'indice':0, 'thread_id': {'$in': list_to_update}}, {'$set': {'updated':'yes'}})

you can pass upsert=Falseif you dont need upsert. Link to documentation: here

upsert=False如果您不需要upsert,您可以通过。文档链接: 这里