简体   繁体   English

Pymongo多更新查询

[英]Pymongo multi update query

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列表,我想将'updated'字段插入'yes'

I am receiving the following error 我收到以下错误

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

Any ideas? 有任何想法吗?

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=False if you dont need upsert. 如果你不需要upsert,你可以传递upsert=False Link to documentation: here 链接到文档: 这里

Use the $set operator: 使用$set运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM