JSON Data in MongoDB

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

JSON is known as JavaScript Object Notation. It is a lightweight data-interchange format and is easily read or written by humans for machines. It is generally used to transmit data between a server and a web application. It is a subset of JavaScript language but it is language-independent. In JSON format, the data is represented in key-value pairs, where the key and value can be of any valid JSON data type such as string, integer, boolean, object, array, etc. For example:

Here is the simple JSON object:

{
“Name”: “Suman”,
“City”: “Delhi”,
“Year”: 2012,
“Age”: 24
}

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:

{
“Id”: 1002,
“Name”: “Mohit”,
“Year”: 2019,
“City”: “Mumbai”,
“Branch”: CSE,
}

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:

  1. Efficiency: BSON is a binary-encoded format that makes data storage and transmission more compact and efficient as compared to JSON format.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *