In this guide, you can learn how to create BSON documents, read BSON from a file, and write BSON to a file by using PyMongo.
BSON, or Binary JSON, is the data format that MongoDB uses to organize and store data. This data format includes all JSON data structure types and adds support for types including dates, different size integers, ObjectIds, and binary data. You can use BSON documents in your Python application by including the bson package. For a complete list of supported types, see the BSON Types server manual page.
BSON documents are stored in MongoDB collections in binary format, while PyMongo represents BSON documents as Python dictionaries. PyMongo automatically converts Python dictionaries into BSON documents when inserting them into a collection. Likewise, when you retrieve a document from a collection, PyMongo converts the BSON document back into a Python dictionary.
The following example shows a document in both dictionary and BSON formats. Use the Dictionary or BSON tab to see the corresponding format:
\x16\x00\x00\x00 # total document size\x02 # 0x02 = type Stringhello\x00 # field name\x06\x00\x00\x00world\x00 # field value\x00 # 0x00 = type EOO ("end of object")
The code samples in this guide use the following BSON document as an example:
{ "address" : { "street" : "Pizza St", "zipcode" : "10003" }, "coord" : [-73.982419, 41.579505] "cuisine" : "Pizza", "name" : "Mongo's Pizza"}
You can create a BSON document by using the same notation you use to create a dictionary in Python. The following example creates a BSON document that represents the preceding sample BSON document:
document = { "address": { "street": "Pizza St", "zipcode": "10003" }, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"}
You can modify the contents of a BSON document by using the same notation you use to modify a dictionary in Python. The following example makes three changes to the sample BSON document:
Adds a new field, restaurant_id
, with the value 12345
Removes the cuisine
field
Sets the value of the name
field to "Mongo's Pizza Place"
document["restaurant_id"] = 12345del document["cuisine"]document["name"] = "Mongo's Pizza Place"
To write BSON data to a file, open a file stream in write-binary mode on the output file. Then, write each document to the output file. Ensure that documents are encoded in BSON format by using the bson.encode()
method.
The following example writes the sample BSON document to file.bson
:
with open("file.bson", "w") as file: file.write(bson.encode(document))
To read BSON documents from a file, open a file stream in read-binary mode on the input file. Then, decode the documents from BSON format as you read them by using the bson.decode()
method.
The following example reads the sample BSON document from file.bson
:
with open("file.bson", "rb") as file: data = file.read() document = bson.decode(data) print(document)
{"address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"}
PyMongo supports the usage of raw BSON documents. The following list contains some situations that might require using raw BSON documents:
Moving a document between databases or collections
Writing binary data to a disk
Bypassing the performance overhead of converting to and from Python dictionaries
The RawBSONDocument
class is a representation of a BSON document that provides access to the underlying raw BSON bytes. To use RawBSONDocument
objects to represent documents in your collection, set the document_class
parameter of the MongoClient
constructor to RawBSONDocument
.
RawBSONDocument
objects are read-only. To modify a RawBSONDocument
, you must first convert it to a Python dictionary.
The following example configures a MongoClient
object to use RawBSONDocument
objects to model the collection, then retrieves the sample document from the preceding examples. Select the Synchronous or Asynchronous tab to see the corresponding code.
from bson.raw_bson import RawBSONDocumentclient = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)collection = client.sample_restaurants.restaurantsraw_doc = collection.find_one({"name": "Mongo's Pizza"})print(type(raw_doc))
<class 'bson.raw_bson.RawBSONDocument'>
from bson.raw_bson import RawBSONDocumentclient = pymongo.AsyncMongoClient("<connection URI>", document_class=RawBSONDocument)collection = client.sample_restaurants.restaurantsraw_doc = await collection.find_one({"name": "Mongo's Pizza"})print(type(raw_doc))
<class 'bson.raw_bson.RawBSONDocument'>
To learn more about any of the methods or types discussed in this guide, see the bson API documentation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4