MongoDb : stuffs you should to know - Part 1

ahtrahdis7

Sidhartha Mallick

Posted on January 7, 2022

MongoDb : stuffs you should to know - Part 1

What is MongoDB ?

MongoDb is a NoSQL document database where data is stored as documents inside collections.
A document is a way to organise data as a set of key-value pairs.
A bunch of documents organised under certain rules or specs is called a collection.
In MongoDb, all data is stored in BSON (Binary JSON).

How does MongoDB Work ?

A hosted mongodb instance is a part of a cluster which contains multiple such servers/instances called as replica-set.

They follow the master-slave architecture as it's internal working.

Working of the master-slave:

In short, a master instance interacts with the outside world to serve any kind of data related requests and the syncs up with the slave instances. In case the master goes down, from the remaining slaves, one instance gets elected to be the master instance and the work continues. It's like, no instance ever went down.

Then, the master instance interacts with a mongo server, which actually talks to all the requests coming it's way. Kind of a proxy, more of a controller of all requests.

Why BSON ?

It is optimised for speed, space and flexibility. BSON’s binary structure encodes type and length information, which allows it to be parsed much more quickly.

BSON supports non-JSON-native data types, like dates and binary data. read more about it

Import/Export

For this, you need to know 4 different commands, two for each.

Export :

  • mongodump : Upon successful execution, it outputs the data in BSON format.
mongodump --uri="atlas cluster uri"
--collection="coll-name" 
--out="output-file"
Enter fullscreen mode Exit fullscreen mode
  • mongoexport : It outputs the data in JSON format.
mongoexport --uri="atlas cluster uri" 
--collection="coll-name" 
--out="output_file.json"
Enter fullscreen mode Exit fullscreen mode

Import :

  • mongorestore : It is used to load bson data dump into a mongodb database.
mongorestore --uri="atlas cluster uri" 
--drop="path to dump"
Enter fullscreen mode Exit fullscreen mode
  • mongoimport: It is used to load json data export into a mongodb database.
mongorestore --uri="atlas cluster uri" 
--drop="path to json file"
--collection="coll-name"
Enter fullscreen mode Exit fullscreen mode

Data Types in MongoDB

datatypes in mongodb - link

Apart from the standard datatypes, mongodb has some special datatypes which enables a lots of features in it.

Especially, ObjectId.

So, The question comes to mind, What is a ObjectId datatype in mongodb ?

Let's see what it is.
It returns a new ObjectId value. The 12-byte ObjectId value consists of:

  • 4-byte timestamp value
  • 5-byte random value generated once per process
  • 3-byte incrementing counter

A special objectid is _id :
In every document, it's like the primary key equivalent for the documents or sub-documents, in the mongodb database.

Every document in a collection has to have a unique _id value. It has a special place in the whole mongodb database architecture.

It's been a lot of information in a single blog. Will continue this in the next part. Until then, Kudos to you, You have learn't the internal workings of mongodb.

Signing Off, ! Happy Coding !

Reach out to me @mallicksidhartha7@gmail.com

💖 💪 🙅 🚩
ahtrahdis7
Sidhartha Mallick

Posted on January 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related