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
May be interested
- Instructions on 2 ways to install MongoDB on Raspberry Piin this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
- Projection in MongoDBin mongodb, projection's meaning is to select only the necessary data instead of selecting the entire data of a document. if a document has 5 fields and you only need 3 fields, you should only select 3 fields from that document.
- How to save React form data in Mongo Databasetry adding mongodb to your web stack to see how easy it is to store and query form data!
- Create Backup in MongoDBto create a database backup in mongodb, you should use the mongodump command. this command will dump all server data into dump directory. there are many options available from which you can limit the amount of data or backup created by remote server.
- GridFS in MongoDBgridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
- Covered Query in MongoDBwhen all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
- 20 free 3D modeling software3d modeling tools help turn personal ideas into beautiful models and prototypes in many areas. here is a list of 20 3d modeling software for individuals or professional users.
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.