Updated March 14, 2023
Introduction to MongoDB JSON
MongoDB JSON is the lightweight interchange format; we can easily transfer MongoDB JSON from one system to other. Also, we can easily read and write the file; the abbreviated name of MongoDB JSON is JavaScript object notation. In MongoDB, high-level JSON has two entities. First is an object, and the second is an array; the object is nothing but the value pair collection, and the array is a list of order values; using these two entities, we can develop complete documents in MongoDB. While creating a JSON object is started with braces and then comes key and value.
Syntax of MongoDB JSON
Given below is the syntax mentioned:
1. MongoDB JSON document structure syntax.
{
Field1: Value1
Field2: Value2
…..
FieldN: ValueN
}
- In the above syntax, field1 to fieldN contains the field which was we have used in the JSON documents.
- Value1 to ValueN is the value of the JSON field.
2. Export the MongoDB collection into a JSON file.
Mongoexport –collection = collection_name –db = db_name –out = filename.JSON
- In the above syntax, we are creating the dump file.
- The parameter mongoexport is used to export the collection into the JSON file.
- Collection parameter is used to export the specified collection into the file.
- DB parameter is used to export the specified database collection into the file.
- Out parameter is the default that we need to use when exporting any collection in MongoDB.
- The filename is the name of the JSON dump file.
3. Import the MongoDB JSON file.
Mongoimport –collection = collection_name –db = db_name –username <name_of_user> --password –file filename.JSON
- In the above syntax, we are importing the JSON dump file into the specified collection.
- The parameter mongoimport is used for import the JSON file data.
- The collection parameter is used to import the data into the specified collection from the file.
- DB parameter is used to import the data into a specified database collected from the file.
How JSON Works in MongoDB?
It is the plain text which was written in JavaScript object notation. We can use it to send the data between one computer to another computer. The use of JSON in MongoDB is very easy; also, we can use the JavaScript built-in function to convert the JSON strings into the object of JavaScript’s.
There is two built-in functions:
- JSON.parse ()
- JSON. Stringify ()
It supports all the data types.
Below data type is supported by MongoDB JSON:
- Number
- Array
- Boolean
- String
It makes the notation of key-value pair using strings, and it will easily be exported and imported into the various tools. The important function of JSON is to transmit the data between web applications and servers. It is basically used the alternate of an XML, which is the language-independent data format. It has a UTF-8 string format. Thus, humans and machines both understand and read the data of files.
It provides a flexible database and schema design as compared to the tabular data model, which was used in relational database models. Basically, documents are polymorphic; the fields can vary from one document to another within the same collection. Using it, we have no need to create the structure of documents for the database. We can directly start our development without creating any structure.
Examples of MongoDB JSON
Different examples are mentioned below:
Example #1
Insert the data using string data types.
In the below example, we have inserted the string value name as ABC into the MongoDB_JSON collection. Thus, the name attribute shows the field, and the ABC string shows the value of the field.
Code:
db.MongoDB_JSON.insert ({name: "ABC"})
db.MongoDB_JSON.find ().pretty ()
Output:
Example #2
Insert the data using numeric data types.
In the below example, we have inserted the numeric value emp_id as 101 into the MongoDB_JSON collection. Thus, the Emp_id attribute shows the field, and 101 integers are shown the value of a field.
Code:
db.MongoDB_JSON.insert ({emp_id: 101})
db.MongoDB_JSON.find ().pretty ()
Output:
Example #3
Insert the data using array data types.
In the below example, we have inserted the array value into the MongoDB_JSON collection. Therefore, we have to assign MongoDB_JSON the same name as the field and the value.
Code:
var MongoDB_JSON = ["MongoDB is NoSQL DB", "MySQL is OpenSource DB", "PostgreSQL is object RDBMS"]
db.MongoDB_JSON.insert ({MongoDB_JSON: MongoDB_JSON})
db.MongoDB_JSON.find ().pretty ()
Output:
Example #4
Insert the data using Boolean data types.
In the below example, we have inserted the Boolean value name as true and the middlename as false into the MongoDB_JSON collection. Name and middlename attribute shows the field, and true, false Boolean value shows the value of the field.
Code:
> db.MongoDB_JSON.insert ({name: true, middlename: false})
> db.MongoDB_JSON.find ().pretty ()
Output:
Example #5
MongoDB export into the JSON file.
Below example shows export MongoDB_JSON collection into the MongoDB_JSON. Json file.
After exporting the data into the JSON file, we can see this file using the cat command. This data comes in a human-readable format.
Code:
[root@localhost ~]# mongoexport --collection=MongoDB_JSON --db=test --out=MongoDB_JSON. Json
[root@localhost ~]# cat MongoDB_JSON. Json
Output:
Example #6
MongoDB import from the JSON file.
The below example shows that import the data into the Mongo_JSON_NEW collection from the MongoDB_JSON. Json file.
Code:
[root@localhost ~]# mongoimport --db test --collection Mongo_JSON_NEW --file MongoDB_JSON.json
[root@localhost ~]# mongo
db.Mongo_JSON_NEW.find().pretty()
Output:
Conclusion
They have their multiple data types are available in MongoDB; using this datatype, we can insert the data into the collection. We can import the data into the collection from JSON file using mongoimport; we can also export the collected data into the JSON file using mongoexport.
Recommended Articles
This is a guide to MongoDB JSON. Here we discuss the introduction, how JSON works in MongoDB? and examples for better understanding. You may also have a look at the following articles to learn more –