MongoDB is obviously a database and its so-called NoSQL database. In Mongo, each database can contain one or more collections. Think of a collection as a table of data. Then each collection can contain one or more structures called documents. Think of document as a row in a table. So, each document contains the data about one single entity. For instance,
i. A blog post
ii. A user
iii. A review, etc.
Now the collection is like the parent structure that contains all these entities. For instance,
i. A blog collection for all posts
ii. Users’ collection
iii. Review’s collection.
The document has a data format that looks a lot like JSON which makes work a lot easier when dealing with them.
MONGOS’ MAIN FEATURES
According to MongoDB website, MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need.
1. Document Based: MongoDB stores data in documents which are field-value pair data structures like JSON. It stores data in documents instead of rows in a table. It is therefore a NoSQL based database and not a relational one.
2. Scalability: Mongo has built-in scalability making it easy to distribute data across multiple machines as your app gets more and more users and starts generating a ton of data. So, whatever you do, MongoDB will make it easy for you to grow.
3. Flexibility: There is no need to define a document data schema before filling it with data, meaning that each document can have a different number and type of fields and we can also change these fields all the time. All these are really in line with some real-world business situations and therefore can become pretty useful.
4. Performant: MongoDB is a very performant system, thanks to features like embedded data model, indexing, shading, flexible document, native duplication and so much more.
Finally, MongoDB is a free open-source database published under SSPL license. It is a great database to build many types of modern, scalable and flexible web applications and mostly used with NodeJS.
DOCUMENTS, BSON AND EMBEDDING.
Mongo uses a data format similar to JSON for that storage called BSON. It looks basically the same as JSON but its typed, meaning that all values must have a data type such as String, Boolean, Date, Integer, Double or more. This means all Mongo documents will actually be typed which is different from JSON.
Just like JSON, BSON documents also have fields and data is stored in key value pairs. Each field is called a column. While we can have multiple values in a field, we can’t have that in a relational database and we would have to find workarounds for this which could then involve more work and more overall complication.
Another extremely important feature in MongoDB is the concept of embedded documents which is again something not present in relational database. So, the process of embedding or de-normalizing is basically to include or embed some related data all into one single document. All these makes a database performant in some situations because this way, it can be easier to read all the data that we need all at once.
Normalizing is how data is modeled in relational databases. Its not possible to embed data. The solution is to create a whole new table for the model and then join the tables by referencing to the ID fields of the related model.
Currently, the maximum size for each BSON document is 16MB but this might increase in the future and each document contains a unique ID which acts as the primary key of that document. Its automatically generated with the Object ID, data type each time there is a new document.