MongoDB Lab-Introduction to JSON and BSON
MongoDB Lab-Introduction to JSON and BSON
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.
{
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}
3. What is BSON?
BSON (Binary JSON) is a binary-encoded version of JSON used by MongoDB for efficient
storage and querying.
MongoDB supports binary data storage using the BinData type. However, for large
multimedia files, MongoDB provides GridFS for efficient storage and retrieval.
db.media.insertOne({
"name": "Profile Picture",
"type": "image/png",
"data": BinData(0, "iVBORw0KGgoAAAANSUhEUgAAB...")
})
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.
MongoDB provides the .pretty() function to convert BSON to a readable JSON format.
Example Command:
db.students.find().pretty()
[
{
"_id": ObjectId("60f7cbbf8b1e7b1f2d3b1a72"),
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}
]
8. Conclusion
Understanding JSON and BSON helps developers efficiently work with MongoDB,
ensuring optimized storage and retrieval of data.