0% found this document useful (0 votes)
5 views

MongoDB Lab-Introduction to JSON and BSON

MongoDB utilizes BSON (Binary JSON) for data storage, which is optimized for performance and supports additional data types, while JSON is used for data exchange due to its human-readable format. Key differences include BSON being binary-based and more compact, leading to faster query execution compared to JSON. MongoDB also employs GridFS for efficient storage of large multimedia files, and automatically converts JSON to BSON during data insertion.

Uploaded by

bindushreekj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MongoDB Lab-Introduction to JSON and BSON

MongoDB utilizes BSON (Binary JSON) for data storage, which is optimized for performance and supports additional data types, while JSON is used for data exchange due to its human-readable format. Key differences include BSON being binary-based and more compact, leading to faster query execution compared to JSON. MongoDB also employs GridFS for efficient storage of large multimedia files, and automatically converts JSON to BSON during data insertion.

Uploaded by

bindushreekj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Understanding JSON and BSON in MongoDB

1. Introduction

MongoDB is a NoSQL database that uses BSON (Binary JSON) for data storage and retrieval.
While JSON (JavaScript Object Notation) is used for data exchange and readability, BSON
is optimized for performance and supports additional data types.

2. What is JSON?

JSON (JavaScript Object Notation) is a lightweight, human-readable format used for storing
and exchanging data between web applications and servers.

Example of JSON Document:

{
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}

Key Features of JSON:

 Human-readable and easy to understand.


 Lightweight and commonly used for APIs.
 Supports basic data types (String, Number, Boolean, Object, Array, Null).
 Text-based format (UTF-8 encoding).

3. What is BSON?

BSON (Binary JSON) is a binary-encoded version of JSON used by MongoDB for efficient
storage and querying.

Example of BSON in MongoDB (Equivalent to JSON Above):


{
"_id": ObjectId("60f7cbbf8b1e7b1f2d3b1a72"),
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}

Key Features of BSON:

 Binary format (optimized for performance).


 More compact than JSON, leading to efficient storage.
 Supports additional data types (e.g., ObjectId, Date, Decimal128, Binary Data).
 Faster read/write operations in MongoDB.

4. Key Differences Between JSON and BSON

Feature JSON BSON (Used in MongoDB)

Format Text-based Binary-based

Readability Human-readable Machine-optimized

Limited (String, Number, Boolean, Extended (ObjectId, Date, Binary,


Data Types
Object, Array, Null) Decimal128, Code)

Size Larger Smaller (compressed)

Performance Slower for large datasets Faster query execution

5. Why MongoDB Uses BSON Instead of JSON?

MongoDB uses BSON for storage because:

1. Efficient Query Execution – BSON enables faster searches and indexing.


2. Compact Storage – Uses less space compared to JSON.
3. Supports Additional Data Types – MongoDB can store ObjectId, Date, Binary
Data, etc.
4. Faster Data Processing – BSON is optimized for reading and writing operations.

6. Storing Multimedia (Images, Audio, Video) in BSON

MongoDB supports binary data storage using the BinData type. However, for large
multimedia files, MongoDB provides GridFS for efficient storage and retrieval.

Example: Storing an Image in BSON

db.media.insertOne({
"name": "Profile Picture",
"type": "image/png",
"data": BinData(0, "iVBORw0KGgoAAAANSUhEUgAAB...")
})

Using GridFS for Large Media Files

For files larger than 16MB, MongoDB provides GridFS, which splits files into smaller
chunks and stores them efficiently.

 GridFS (Grid File System) is a distributed file storage system within MongoDB.
 It splits large files into smaller 255KB chunks and stores them across multiple documents.

 It provides efficient retrieval, streaming, and storage of large files.

Command to Store an Image in GridFS:

mongofiles -d my_database put image.png

Command to Retrieve the File:

mongofiles -d my_database get image.png


7. How to Convert BSON to JSON in MongoDB?

MongoDB provides the .pretty() function to convert BSON to a readable JSON format.

Example Command:

db.students.find().pretty()

Output in JSON Format:

[
{
"_id": ObjectId("60f7cbbf8b1e7b1f2d3b1a72"),
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}
]

8. Conclusion

 JSON is used for data exchange (human-readable format).


 BSON is used for data storage in MongoDB (compact and optimized).
 MongoDB automatically converts JSON to BSON when inserting data.
 For large multimedia files, MongoDB uses GridFS instead of BSON.

Understanding JSON and BSON helps developers efficiently work with MongoDB,
ensuring optimized storage and retrieval of data.

You might also like