JSON Data in MongoDB
In this article, we will discuss JSON Data in MongoDB with Examples. Please read our previous article where we discussed MongoDB Less Than ($lt) Operator with Examples.
JSON Data in MongoDB
Here is the simple JSON object:
In the above example, Name, City, Year, and Age are the keys, and Suman, Delhi, 2012, and 24 are their corresponding values. Also, you are allowed to nest objects and arrays with each other.
Due to its simplicity, flexibility readability, and ease of use of various programming languages, it is commonly used in web APIs, data storage formats, configuration files, log messages, etc.
The connection between MongoDB and JSON
JSON and MongoDB have a very close relationship with each other because MongoDB is a document-oriented database, so it uses a JSON-like format known as BSON format to store and represent data. BSON is a superset of JSON or also known as extended JSON with some additional data types.
While working with MongoDB you will interact with JSON-like documents where each document is a collection is key-value pairs. These documents are further stored in a collection. For example:
Here is the simple JSON-like document in MongoDB:
Here Id represents the unique identifier for the document and name, year, city, and branch represent the fields- value pairs. You can also perform CURD (create, update, read, delete) operations on these objects.
Why MongoDB uses BSON over JSON?
MongoDB uses BSON as its data storage format over JSON due to various reasons and some of the reasons are as follows:
- Efficiency: BSON is a binary-encoded format that makes data storage and transmission more compact and efficient as compared to JSON format.
- Richer Data Types: BSON is an extended JSON format so it includes additional data types like Date, Binary, ObjectId, and much more. With the help of these data types, MongoDB handles a broad range of data and provides good support to complex data structures.
- Indexing and Querying: BSON supports various indexing and querying features due to which we can easily perform database operations. It also includes array indexing and efficient storage for numeric data types.
- Seamless Integration: BSON is specially designed for MongoDB which allows it to integrate seamlessly with other database features. For example, replication and sharing features directly work with BSON documents.
- Performance: BSON is faster in parsing and serialization as compared to JSON. Due to this it improves the read and write performance of MongoDB while working with large amounts of data.
Due to these advantages, MongoDB uses BSON over JSON format. BSON provides a much more efficient and flexible data storage format as compared to JSON.