You are reading the article How Json Works In Mongodb With Examples updated in September 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Json Works In Mongodb With Examples
Introduction to MongoDB JSONMongoDB 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.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Syntax of MongoDB JSON
Given below is the syntax mentioned:
1. MongoDB JSON document structure syntax.
}
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.
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.
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 JSONDifferent examples are mentioned below:
Example #1Insert 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.find ().pretty ()
Output:
Example #2Insert 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:
Output:
Example #3Insert 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.find ().pretty ()
Output:
Example #4Insert 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.find ().pretty ()
Output:
Example #5Below 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:
[[email protected] ~]# cat MongoDB_JSON. Json
Output:
Example #6MongoDB 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:
db.Mongo_JSON_NEW.find().pretty()
Output:
ConclusionThey 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 ArticlesThis 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 –
You're reading How Json Works In Mongodb With Examples
Update the detailed information about How Json Works In Mongodb With Examples on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!