MongoDb : stuffs you should to know - Part 1
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
A bunch of documents organised under certain rules or specs is called a collection.
In MongoDb, all data is stored in BSON (Binary JSON).
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.
It is optimised for
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
binary data. read more about it
For this, you need to know 4 different commands, two for each.
mongodump: Upon successful execution, it outputs the data in BSON format.
mongodump --uri="atlas cluster uri" --collection="coll-name" --out="output-file"
mongoexport: It outputs the data in JSON format.
mongoexport --uri="atlas cluster uri" --collection="coll-name" --out="output_file.json"
mongorestore: It is used to load bson data dump into a mongodb database.
mongorestore --uri="atlas cluster uri" --drop="path to dump"
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"
Apart from the standard datatypes, mongodb has some special datatypes which enables a lots of features in it.
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
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 @[email protected]