Python operation MongoDB Five large write operations

Source: Internet
Author: User
Tags pprint

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.