Data modeling in MongoDB
Data in MongoDB has a flexible Schema. Documents in the same Collection need not have the same set of fields or structures, and common fields in Collection Documents can keep different data types.
Some pay attention while designing schema in MongoDB
Design your Schema according to user requirements.
Combine objects into a Document if you will use those objects together. If not, you should distinguish them (but make sure you don't need to use Join).
Copy data (but there is a limit) because disk space is nothing when compared to calculation time.
Perform Join while recording, do not perform while reading.
Optimize your Schema with frequently used cases.
Perform complex Aggregation in that Schema.
For example
Suppose, a customer needs a database design for his blog site, and below, you see the differences between the Schema design of RDBMS and MongoDB. This website has the following requirements:
Each post has a unique title, description and Url.
Each post may have one or more tags.
Each post has the name of the poster and the total number of likes.
Each post has comments provided by the user along with their name, message, time, and likes.
On each post, there can be 0 or more comments.
In the Schema design of the RDBMS for the above requirements there will be at least three data tables:
While MongoDB's Schema design will only have one Collection Post with the following structure:
{ _id : POST_ID title : TITLE_OF_POST , description : POST_DESCRIPTION , by : POST_BY , url : URL_OF_POST , tags : [ TAG1 , TAG2 , TAG3 ], likes : TOTAL_LIKES , comments : [ { user : 'COMMENT_BY' , message : TEXT , dateCreated : DATE_TIME , like : LIKES }, { user : 'COMMENT_BY' , message : TEXT , dateCreated : DATE_TIME , like : LIKES } ] }
So while displaying data, in RDBMS you need to combine three tables and in MongoDB will only need to display from a Collection.
According to Tutorialspoint
Previous post: Install MongoDB
Next lesson: Create Database in MongoDB
You should read it
- Advantages of MongoDB
- MongoDB queries and tasks you should know
- Data type in MongoDB
- Index (Mong) in MongoDB
- Shard in MongoDB
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- ObjectId in MongoDB
- Query Document in MongoDB
- Map Reduce in MongoDB
- Aggregation in MongoDB
- MongoDB malicious code attacks more than 26,000 victims in a week
- Learn about security features and authentication in MongoDB