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 GridFSPyMongo supports CPython 3.9+ and PyPy3.10+.
Only one intentional change. Instances ofbytes
are encoded as BSON type 5 (Binary data) with subtype 0.In Python 3 they are decoded back tobytes
. InPython 2 they are decoded toBinary
with subtype 0.
For example, let’s insert abytes
instance using Python 3 thenread it back. Notice the byte string is decoded back tobytes
:
Python3.7.9(v3.7.9:13c94747c7,Aug152020,01:31:08)[Clang6.0(clang-600.0.57)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>importpymongo>>>c=pymongo.MongoClient()>>>c.test.bintest.insert_one({'binary':b'this is a byte string'}).inserted_idObjectId('4f9086b1fba5222021000000')>>>c.test.bintest.find_one(){'binary':b'this is a byte string','_id':ObjectId('4f9086b1fba5222021000000')}
Now retrieve the same document in Python 2. Notice the byte string is decodedtoBinary
:
Python2.7.6(default,Feb262014,10:36:22)[GCC4.7.3]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>importpymongo>>>c=pymongo.MongoClient()>>>c.test.bintest.find_one(){u'binary':Binary('this is a byte string',0),u'_id':ObjectId('4f9086b1fba5222021000000')}
There is a similar change in behavior in parsing JSON binary with subtype 0.In Python 3 they are decoded intobytes
. In Python 2 they aredecoded toBinary
with subtype 0.
For example, let’s decode a JSON binary subtype 0 using Python 3. Notice thebyte string is decoded tobytes
:
Python3.7.9(v3.7.9:13c94747c7,Aug152020,01:31:08)[Clang6.0(clang-600.0.57)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>frombson.json_utilimportloads>>>loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}'){'b':b'this is a byte string'}
Now decode the same JSON in Python 2 . Notice the byte string is decodedtoBinary
:
Python2.7.10(default,Feb72017,00:08:15)[GCC4.2.1CompatibleAppleLLVM8.0.0(clang-800.0.34)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>frombson.json_utilimportloads>>>loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}'){u'b':Binary('this is a byte string',0)}
Instances ofObjectId
pickled using Python 2can always be unpickled using Python 3.
If you pickled an ObjectId using Python 2 and want to unpickle it usingPython 3 you must passencoding='latin-1'
to pickle.loads:
Python2.7.6(default,Feb262014,10:36:22)[GCC4.7.3]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>importpickle>>>frombson.objectidimportObjectId>>>oid=ObjectId()>>>oidObjectId('4f919ba2fba5225b84000000')>>>pickle.dumps(oid)'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...'Python3.7.9(v3.7.9:13c94747c7,Aug152020,01:31:08)[Clang6.0(clang-600.0.57)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>importpickle>>>pickle.loads(b'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...',encoding='latin-1')ObjectId('4f919ba2fba5225b84000000')
If you need to pickle ObjectIds using Python 3 and unpickle them using Python 2you must useprotocol<=2
:
Python3.7.9(v3.7.9:13c94747c7,Aug152020,01:31:08)[Clang6.0(clang-600.0.57)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>importpickle>>>frombson.objectidimportObjectId>>>oid=ObjectId()>>>oidObjectId('4f96f20c430ee6bd06000000')>>>pickle.dumps(oid,protocol=2)b'\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...'Python2.7.15(default,Jun212018,15:00:48)[GCC7.3.0]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>importpickle>>>pickle.loads('\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...')ObjectId('4f96f20c430ee6bd06000000')