我想更新mongo集合中的数千个文档.我想使用ObjectId查找它们,然后无论哪个文档匹配,都应进行更新.对于所有文档,我的更新都是相同的.我有ObjectId的列表.对于列表中的每个ObjectId,mongo应该找到匹配的文档并将该文档的"isBad"键更新为"N"
I want to update thousands of documents in mongo collection. I want to find them using ObjectId and then whichever document matches , should be updated. My update is same for all documents. I have list of ObjectId. For every ObjectId in list, mongo should find matching document and update "isBad" key of that document to "N"
ids = [ObjectId('56ac9d3fa722f1029b75b128'), ObjectId('56ac8961a722f10249ad0ad1')] bulk = db.testdata.initialize_unordered_bulk_op() bulk.find( { '_id': ids} ).update( { '$set': { "isBad" : "N" } } ) print bulk.execute()这给了我结果:
{'nModified': 0, 'nUpserted': 0, 'nMatched': 0, 'writeErrors': [], 'upserted': [], 'writeConcernErrors': [], 'nRemoved': 0, 'nInserted': 0}这是预期的,因为它试图将"_id"与列表匹配.但是我不知道该怎么办.
This is expected because it is trying to match "_id" with list. But I don't know how to proceed.
我知道如何分别更新每个文档.我的列表大小约为25000.我不想单独进行25000个呼叫.我的收藏中的文件数量更多.我正在使用python2,pymongo = 3.2.1
I know how to update every document individually. My list size is of the order of 25000. I do not want to make 25000 calls individually. Number of documents in my collection are much more. I am using python2, pymongo = 3.2.1.
推荐答案我得到了答案,可以这样完成:
I got the answer, It can be done like this :
bulk = db.testdata.initialize_unordered_bulk_op() for i in range (0, len(ids)): bulk.find( { '_id': ids[i]}).update({ '$set': { "isBad" : "N" }}) print bulk.execute()更多推荐
使用多个ObjectId在Pymongo中进行批量更新
发布评论