pymongo

予早 2026-04-30 23:53:28
Categories: Tags:

https://pymongo.readthedocs.io/en/stable/api/index.html

连接池大小默认最小0最大100

PyMongo 函数**bulk_write()**向服务器发送一批写操作。批量执行写操作可以提高写吞吐量。

 用法: bulk_write(requests, ordered = True, bypass_document_validation = False, session = None)
 参数:
 # importing the module
 from pymongo import MongoClient, InsertOne, DeleteOne, ReplaceOne
   
 # creating a MongoClient object 
 client = MongoClient() 
      
 # connecting with the portnumber and host 
 client = MongoClient("mongodb://localhost:27017/") 
    
 # accessing the database 
 database = client['database']   
    
 # access collection of the database 
 mycollection = mydatabase['myTable'] 
   
 # defining the requests
 requests = [InsertOne({"Student name":"Cody"}),
             InsertOne({ "Student name":"Drew"}),
             DeleteOne({"Student name":"Cody"}),
             ReplaceOne({"Student name":"Drew"},
                        { "Student name":"Andrew"}, upsert = True)]
   
 # executing the requests
 result = mycollection.bulk_write(requests)
   
 for doc in mycollection.find({}):
     print(doc)
 # importing the modules
 from pymongo import MongoClient, InsertOne, DeleteOne, ReplaceOne, UpdateOne
   
 # creating a MongoClient object 
 client = MongoClient() 
      
 # connecting with the portnumber and host 
 client = MongoClient("mongodb://localhost:27017/") 
    
 # accessing the database 
 database = client['database']   
    
 # access collection of the database 
 mycollection = mydatabase['myTable']  
   
 # defining the requests
 requests = [InsertOne({ "x":5}),
             InsertOne({ "y":2}),
             UpdateOne({'x':5}, {'$inc':{'x':3}}),
             DeleteOne({ "y":2})]
   
 # executing the requests
 result = mycollection.bulk_write(requests)
   
 for doc in mycollection.find({}):
     print(doc)

bulk_write(requests, ordered=True, bypass_document_validation=False, session=None)