Updated March 6, 2023
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:
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:
[root@localhost ~]# mongoexport –collection = MongoDB_Update –db = test –out = MongoDB_BSON.bson
[root@localhost ~]# ls -lrt MongoDB_BSON.bson
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:
[root@localhost ~]# mongoimport --db test --collection MongoDB_Update_Test --file MongoDB_BSON.bson
[root@localhost ~]# mongo
db.MongoDB_Update_Test.find ()
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 ()
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 ()
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 –