문제

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?

도움이 되었습니까?

해결책

Use the $set operator:

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top