bson
– BSON (Binary JSON) Encoding and Decodingbinary
– Tools for representing binary data to be stored in MongoDBcode
– Tools for representing JavaScript codecodec_options
– Tools for specifying BSON codec optionsdatetime_ms
– Support for BSON UTC Datetimedbref
– Tools for manipulating DBRefs (references to documents stored in MongoDB)decimal128
– Support for BSON Decimal128errors
– Exceptions raised by thebson
packageint64
– Tools for representing BSON int64json_util
– Tools for using Python’sjson
module with BSON documentsmax_key
– Representation for the MongoDB internal MaxKey typemin_key
– Representation for the MongoDB internal MinKey typeobjectid
– Tools for working with MongoDB ObjectIdsraw_bson
– Tools for representing raw BSON documents.regex
– Tools for representing MongoDB regular expressionsson
– Tools for working with SON, an ordered mappingtimestamp
– Tools for representing MongoDB internal Timestampstz_util
– Utilities for dealing with timezones in Pythonpymongo
– Python driver for MongoDBpymongoasync
– Async Python driver for MongoDBchange_stream
– Watch changes on a collection, database, or clusterclient_session
– Logical sessions for sequential operationscollection
– Collection level operationscommand_cursor
– Tools for iterating over MongoDB command resultscursor
– Tools for iterating over MongoDB query resultsdatabase
– Database level operationsmongo_client
– Tools for connecting to MongoDBauth_oidc
– MONGODB-OIDC Authenticationchange_stream
– Watch changes on a collection, database, or clusterclient_options
– Read only configuration options for a MongoClient.client_session
– Logical sessions for sequential operationscollation
– Tools for working with collations.collection
– Collection level operationscommand_cursor
– Tools for iterating over MongoDB command resultscursor
– Tools for iterating over MongoDB query resultsdatabase
– Database level operationsdriver_info
encryption
– Client-Side Field Level Encryptionencryption_options
– Automatic Client-Side Field Level Encryptionerrors
– Exceptions raised by thepymongo
packagemongo_client
– Tools for connecting to MongoDBmonitoring
– Tools for monitoring driver events.operations
– Operation class definitionspool
– Pool module for use with a MongoDB client.read_concern
– Tools for working with read concern.read_preferences
– Utilities for choosing which member of a replica set to read from.results
– Result class definitionsserver_api
– Support for MongoDB Stable APIserver_description
– An object representation of a server the driver is connected to.topology_description
– An object representation of a deployment of MongoDB servers.uri_parser
– Tools to parse and validate a MongoDB URIwrite_concern
– Tools for specifying write concernevent_loggers
– Example loggersgridfs
– Tools for working with GridFSThis tutorial explains how to take advantage of PyMongo’s bulkwrite operation features. Executing write operations in batchesreduces the number of network round trips, increasing writethroughput.
Added in version 2.6.
A batch of documents can be inserted by passing a list to theinsert_many()
method. PyMongowill automatically split the batch into smaller sub-batches based onthe maximum message size accepted by MongoDB, supporting very largebulk insert operations.
>>>importpymongo>>>db=pymongo.MongoClient().bulk_example>>>db.test.insert_many([{"i":i}foriinrange(10000)]).inserted_ids[...]>>>db.test.count_documents({})10000
Added in version 2.7.
PyMongo also supports executing mixed bulk write operations. A batchof insert, update, and remove operations can be executed together usingthe bulk write operations API.
Ordered bulk write operations are batched and sent to the server in theorder provided for serial execution. The return value is an instance ofBulkWriteResult
describing the type and countof operations performed.
>>>frompprintimportpprint>>>frompymongoimportInsertOne,DeleteMany,ReplaceOne,UpdateOne>>>result=db.test.bulk_write(...[...DeleteMany({}),# Remove all documents from the previous example....InsertOne({"_id":1}),...InsertOne({"_id":2}),...InsertOne({"_id":3}),...UpdateOne({"_id":1},{"$set":{"foo":"bar"}}),...UpdateOne({"_id":4},{"$inc":{"j":1}},upsert=True),...ReplaceOne({"j":1},{"j":2}),...]...)>>>pprint(result.bulk_api_result){'nInserted': 3, 'nMatched': 2, 'nModified': 2, 'nRemoved': 10000, 'nUpserted': 1, 'upserted': [{'_id': 4, 'index': 5}], 'writeConcernErrors': [], 'writeErrors': []}
The first write failure that occurs (e.g. duplicate key error) aborts theremaining operations, and PyMongo raisesBulkWriteError
. Thedetails
attribute ofthe exception instance provides the execution results up until the failureoccurred and details about the failure - including the operation that causedthe failure.
>>>frompymongoimportInsertOne,DeleteOne,ReplaceOne>>>frompymongo.errorsimportBulkWriteError>>>requests=[...ReplaceOne({"j":2},{"i":5}),...InsertOne({"_id":4}),# Violates the unique key constraint on _id....DeleteOne({"i":5}),...]>>>try:...db.test.bulk_write(requests)...exceptBulkWriteErrorasbwe:...pprint(bwe.details)...{'nInserted': 0, 'nMatched': 1, 'nModified': 1, 'nRemoved': 0, 'nUpserted': 0, 'upserted': [], 'writeConcernErrors': [], 'writeErrors': [{'code': 11000, 'errmsg': '...E11000...duplicate key error...', 'index': 1,... 'op': {'_id': 4}}]}
Unordered bulk write operations are batched and sent to the server inarbitrary order where they may be executed in parallel. Any errorsthat occur are reported after all operations are attempted.
In the next example the first and third operations fail due to the uniqueconstraint on _id. Since we are doing unordered execution the secondand fourth operations succeed.
>>>requests=[...InsertOne({"_id":1}),...DeleteOne({"_id":2}),...InsertOne({"_id":3}),...ReplaceOne({"_id":4},{"i":1}),...]>>>try:...db.test.bulk_write(requests,ordered=False)...exceptBulkWriteErrorasbwe:...pprint(bwe.details)...{'nInserted': 0, 'nMatched': 1, 'nModified': 1, 'nRemoved': 1, 'nUpserted': 0, 'upserted': [], 'writeConcernErrors': [], 'writeErrors': [{'code': 11000, 'errmsg': '...E11000...duplicate key error...', 'index': 0,... 'op': {'_id': 1}}, {'code': 11000, 'errmsg': '...', 'index': 2,... 'op': {'_id': 3}}]}
Bulk operations are executed with thewrite_concern
of the collection theyare executed against. Write concern errors (e.g. wtimeout) will be reportedafter all operations are attempted, regardless of execution order.
>>>frompymongoimportWriteConcern>>>coll=db.get_collection(...'test',write_concern=WriteConcern(w=3,wtimeout=1))>>>try:...coll.bulk_write([InsertOne({'a':i})foriinrange(4)])...exceptBulkWriteErrorasbwe:...pprint(bwe.details)...{'nInserted': 4, 'nMatched': 0, 'nModified': 0, 'nRemoved': 0, 'nUpserted': 0, 'upserted': [], 'writeConcernErrors': [{'code': 64... 'errInfo': {'wtimeout': True}, 'errmsg': 'waiting for replication timed out'}], 'writeErrors': []}