简体   繁体   中英

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'

I am receiving the following error

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

Any ideas?

from pymongo 3.1 use 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. Link to documentation: here

Use the $set operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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