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 BSON
Secondary Sidebar
MongoDB Tutorial
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • 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
  • 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
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • 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 BSON

MongoDB BSON

Definition of MongoDB BSON

Mongodb bson stands for binary JSON, it’s superset of JSON types it will include the more advanced datatype which was not present in JSON type. MongoDB stores the collection documents in binary formats this is called binary JSON. There is multiple BSON data types available in MongoDB like double, string, object, array, the data stored in BSON format. We can also store the backup of documents, collection, and databases in BSON format, at the time of exporting the backup we need to define the file type as BSON to store the data in BSON format.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the syntax of MongoDB BSON.

1) MongoDB BSON document structure syntax

{
Field1: Value1
Field2: Value2
…..
FieldN: ValueN
}

2) Export the MongoDB collection into BSON file

Mongoexport –collection = collection_name –db = db_name –out = filename.bson

3) Import the MongoDB BSON file

Mongoimport –collection = collection_name –db = db_name –username <name_of_user> --password –file filename.bson

Parameter:

1) Field 1 to field N – This contains the field which was we have used while inserting the documents into the collection by using BSON data types. We can use multiple fields in a single collection and also give different datatype to different fields.

2) Value1 to ValueN – This is the value which was we have assigned for the BSON documents field.

3) Mongoexport – This parameter is used to export the collection documents of the database into the BSON file. We can export any database or collection in BSON file format. Mongoexport command is used for exporting the data of collection or databases into the BSON format file.

4) Collection – This parameter is used to export the specified collection into the MongoDB BSON file.

5) DB – This parameter is used to export the specified database collection into the MongoDB BSON file. We need to specify dbname while exporting data into BSON file format.

6) Out – This parameter is the default which was we need to use at the time of exporting any collection in MongoDB.

7) Filename – This is defined as the name of BSON dump file. We can specify any name to the BSON file.

8) Mongoimport – This is used to import the BSON file data into the collection or database which was we have defined in our command. Mongoimport command is used for importing the data into the collection by using BSON format file.

How bson work in MongoDB?

It is a binary format type that was used in MongoDB to store the data in binary format. BSON in MongoDB stands for Binary JavaScript Object Notation whereas JSON stands for JavaScript Object Notation. BSON is advanced of JSON in MongoDB, there are multiple advanced datatypes used in BSON.

Below is the datatype of BSON which was available in MongoDB.

1) Null
2) Date
3) Boolean
4) Objectid
5) Undefined
6) Binary data
7) Array
8) Object
9) String
10) Double
11) Max key
12) Min key
13) Decimal 128
14) 64-bit integer
15) Timestamp
16) 32-bit integer
17) Symbol
18) JavaScript code with scope
19) JavaScript
20) DBPointer
21) Regular expression

We can use the above datatype in our collection field using BSON datatype in MongoDB. Type operator is supported values to query the fields while using BSON datatype in MongoDB. The BSON records are small as compared to the JSON records in MongoDB.

The below example shows the how BSON document is stored in the database. Below is an example of a JSON document.

{
“BSON” : “Document”
}

The below format shows how the BSON document is stored in the database.

\x20\x00\x00\x00
\x05
BSON\x00
\x04\x00\x00\x00Document\x00
\x00

First-line indicates the size of the document which was required to store the document. Second-line indicates that string type that we have used to store the document.

Third line is indicates that field name which was we have used in our documents. Fourth line is indicates that field value for the field name which was we have used in our documents.

The last line indicates the end of the object.

We cannot read the BSON data, it is not in human-readable format, the only machines can read the BSON type of data.

Encoding of BSON type format in MongoDB is binary. BSON is supported a more advanced datatype as compared to JSON in MongoDB.

It is the language-independent format of data interchange. BSON is more schema-less as compared to JSON type.

Example

Below is the example of MongoDB BSON.

1) MongoDB export single collection into the BSON file

The below example shows the export of the collection into the BSON file. We have exported the single document name as MongoDB_Update into the BSON file.

After exporting the data into the BSON file we can see this file using ls -lrt command.

Code:

[[email protected] ~]# mongoexport –collection = MongoDB_Update –db = test –out = MongoDB_BSON.bson
[[email protected] ~]# ls -lrt MongoDB_BSON.bson

1

Figure – Example of MongoDB export single collection into the BSON file.

2) MongoDB import the data into collection from the BSON file.

The below example shows that import the data into the MongoDB_Update_Test collection from the MongoDB_BSON.bson file.

Code:

[[email protected] ~]# mongoimport --db test --collection MongoDB_Update_Test --file MongoDB_BSON.bson
[[email protected] ~]# mongo
db.MongoDB_Update_Test.find ()

2

Figure – Example of MongoDB import the data into collection from the BSON file.

3) Insert the documents into the collection by using BSON Timestamp datatype

The below example shows that insert the documents into a collection by using Timestamp datatype. We have inserted timestamp in newDate field.

After inserting the document we can see that it will show acknowledgment as true. We have inserted the document in BSON_TIMESTAMP collection.

Code:

db.BSON_TIMESTAMP.insertOne ( { newDate: new Timestamp() } );
db.BSON_TIMESTAMP.find ()

3

Figure – Example of insert the data into collection by using Timestamp datatype.

4) Insert the data into collection by using BSON Date datatype

Below example shows that insert the documents into the collection by using Date datatype. We have inserted date in the newDate field.

Code:

db.BSON_TIMESTAMP.insertOne ( { newDate: new Date() } );
db.BSON_TIMESTAMP.find ()

4

Figure – Example of insert the documents into collection by using Date datatype.

Conclusion

MongoDB BSON stands for Binary JSON, there is multiple datatypes that were supported by MongoDB BSON. BSON is supporting a more advanced datatype as compared to JSON. BSON type format is encoding in MongoDB. BSON data type is more useful and important to store the documents in binary format.

Recommended Articles

This is a guide to MongoDB BSON. Here we discuss the definition, syntax, How bson work in Mongodb? Examples, and code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB group by
  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