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)
参数:
- **requests:**请求作为写操作实例列表传递。
- ordered:(可选)一个布尔值,指定操作执行是以有序还是无序的方式执行的。默认情况下,它设置为 True。
- bypass_document_validation:(可选)指示是否绕过文档验证的值。
- session:(可选)一个 ClientSession。
# 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)