EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB JSON
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB JSON
    • MongoDB Careers
    • MongoDB Migration
    • MongoDB Export to Excel
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
    • MongoDB schema
    • MongoDB find by id
    • Mongodb max document size
    • Mongodb not equal
    • MongoDB keyfile
    • MongoDB Hosting
    • MongoDB Kafka Connector
    • MongoDB exists
    • MongoDB express
    • MongoDB Performance
    • MongoDB Connection String
    • MongoDB BI Connector
    • Mongodb aggregation pipeline
    • Mongodb backup and restore
    • MongoDB Drop Collection
    • MongoDB Text Search
    • MongoDB URI
    • MongoDB Full Text Search
    • MongoDB List Databases
    • MongoDB Use Cases
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB JSON

MongoDB JSON

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

MongoDB JSON 1

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:

MongoDB JSON 2

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:

MongoDB JSON 3

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:

Boolean data types

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:

[[email protected] ~]# mongoexport --collection=MongoDB_JSON --db=test --out=MongoDB_JSON. Json
[[email protected] ~]# cat MongoDB_JSON. Json

Output:

export

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:

[[email protected] ~]# mongoimport --db test --collection Mongo_JSON_NEW --file MongoDB_JSON.json
[[email protected] ~]# mongo
db.Mongo_JSON_NEW.find().pretty()

Output:

import

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 –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB Date Query
  4. MongoDB Database
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more