Import pymongo# Library Name db = Pymongo. Mongoclient (' 192.168.30.252 ', 27017). Bulk_example#test Collection Insert Db.test.insert_many ([{' I ': i} for I in Xrange (10000)]). Inserted_idsdb.test.count () # Organized large-scale data write bulk = Db.test.initialize_ordered_bulk_op () # Remove all documents from the Previous example.bulk.find ({}). Remove () Bulk.insert ({' _id ': 1}) Bulk.insert ({' _id ': 2}) Bulk.insert ({' _id ': 3}) # Update Bulk.find ({' _id ': 1}). Update ({' $set ': {' foo ': ' Bar '}}) # Insert replace Bulk.find ({' _id ': 4}). Upsert (). Update ({' $inc ': {' J ': 1} }) #替换bulk. Find ({' J ': 1}). Replace_one ({' J ': 2}) #execute是执行result = Bulk.execute () pprint (result) #存在异常 processing from Pymongo.errors Import bulkwriteerror# Cache queue Bulk=db.test.initialize_ordered_bulk_op () #查找j = 2 and Replace with J=5bulk.find ({' J ': 2 }). replace_one ({' I ': 5}) #插入id =4bulk.insert ({' _id ': 4}) Bulk.find ({' I ': 5}). Remove_one () Try:bulk.execute () except Bulkwriteerror as Bwe:pprint (bwe.details) from pymongo.errors import Bulkwriteerrorbulk = db.test.initialize_unordered _bulk_op () Bulk.insert ({' _id ': 1}) Bulk.find ({' _id ': 2}). Remove_one () bUlk.insert ({' _id ': 3}) Bulk.find ({' _id ': 4}). Replace_one ({' I ': 1}) Try:bulk.execute () except Bulkwriteerror as Bwe: Pprint (bwe.details) #初始化队列bulk = Db.test.initialize_ordered_bulk_op () bulk.insert ({' A ': 0}) Bulk.insert ({' A ': 1}) Bulk.insert ({' A ': 2}) Bulk.insert ({' A ': 3}) Try:bulk.execute ({' Wtimeout ': 1}) except Bulkwriteerror as Bwe:pprint ( Bwe.details)
Python operation MongoDB Five large write operations