0% completed
BSON (Binary JSON) is the binary-encoded serialization of JSON-like documents. BSON extends the JSON model to provide additional data types and to be efficient for encoding and decoding within different languages. MongoDB uses BSON as the data storage and network transfer format for documents.
BSON supports a variety of data types, many of which extend beyond the capabilities of JSON. Here is a table of key BSON data types used in MongoDB:
Data Type | Alias | Description |
---|---|---|
String | string | UTF-8 string. |
Integer | int32 , int64 | 32-bit and 64-bit signed integers. |
Double | double | 64-bit IEEE 754 floating point. |
Boolean | bool | true or false . |
Null | null | Null value. |
Array | array | An ordered list of values. |
Object | object | A collection of key-value pairs. |
Binary Data | binData | A byte array. |
ObjectId | objectId | A unique identifier for documents. |
Date | date | A datetime value. |
Regular Expression | regex | A regex pattern. |
JavaScript | javascript | JavaScript code. |
JavaScript with Scope | javascriptWithScope | JavaScript code with a scope. |
Timestamp | timestamp | A special internal timestamp used by MongoDB. |
Decimal128 | decimal128 | A 128-bit decimal-based floating point. |
Min Key | minKey | Special type used for internal comparisons, lower bound. |
Max Key | maxKey | Special type used for internal comparisons, upper bound. |
Here’s an example of how a JSON document is represented in BSON:
JSON Document:
{ "name": "John Doe", "age": 30, "email": "john.doe@example.com", "isStudent": false, "createdAt": "2023-06-01T00:00:00Z" }
Equivalent BSON Representation:
0x71 0x00 0x00 0x00 0x02 0x6e 0x61 0x6d 0x65 0x00 0x09 0x00 0x00 0x00 0x4a 0x6f 0x68 0x6e 0x20 0x44 0x6f 0x65 0x00 0x10 0x61 0x67 0x65 0x00 0x1e 0x00 0x00 0x00 0x02 0x65 0x6d 0x61 0x69 0x6c 0x00 0x15 0x00 0x00 0x00 0x6a 0x6f 0x68 0x6e 0x2e 0x64 0x6f 0x65 0x40 0x65 0x78 0x61 0x6d 0x70 0x6c 0x65 0x2e 0x63 0x6f 0x6d 0x00 0x08 0x69 0x73 0x53 0x74 0x75 0x64 0x65 0x6e 0x74 0x00 0x00 0x02 0x63 0x72 0x65 0x61 0x74 0x65 0x64 0x41 0x74 0x00 0x15 0x00 0x00 0x00 0x32 0x30 0x32 0x33 0x2d 0x30 0x36 0x2d 0x30 0x31 0x54 0x30 0x30 0x3a 0x30 0x30 0x3a 0x30 0x30 0x5a 0x00 0x00
While BSON and JSON serve similar purposes, there are critical differences:
Aspect | BSON | JSON |
---|---|---|
Format | Binary | Text-based |
Efficiency | More efficient in space and speed | Less efficient compared to BSON |
Data Types | Supports more data types (e.g., Date, Binary) | Limited to basic data types |
Traversal | Designed for efficient traversal | Not optimized for traversal |
Human Readability | Less human-readable (binary) | More human-readable (text) |
BSON is a critical component of MongoDB, providing a binary representation of JSON-like documents that is both efficient and capable of supporting a wide range of data types. Understanding BSON and its various data types helps in effectively storing and manipulating data within MongoDB. By leveraging BSON's strengths, you can ensure your MongoDB applications are both performant and capable of handling complex data structures.
.....
.....
.....