Turn from: Easy hundred tutorials
The data in MongoDB has a flexible pattern. The documents are in the same collection, but they do not need to have the same field or structure collection, and the public fields in the collection document can contain different types of data.
The data in MongoDB has a flexible pattern. Unlike SQL databases, SQL databases must determine and declare the schema of the table before inserting data, and MongoDB collections do not enforce the document structure. This flexibility helps map documents to entities or objects. Each document can match a data field that represents an entity, even if the data has a substantial change. However, the document in the collection has a similar structure.
The key challenge in data modeling is to balance the requirements of the application, the performance characteristics of the database engine, and the data retrieval pattern. When designing a data model, always consider the application usage of the data (that is, the query, update, and processing of the data) and the inherent structure of the data itself.
There are some considerations when designing schemas in MongoDB:
- Design the architecture according to user requirements.
- Merge objects into one document, or separate them (but make sure you don't need to connect).
- Copy data (but with restrictions) because disk space is inexpensive compared to calculation time.
- Join when writing, not when reading.
- Optimize the architecture for the most common use cases.
- Perform complex aggregations in a pattern.
Example
Suppose the customer needs the database design of his blog/site and see the difference between the RDBMS and MongoDB architecture design. The website has the following requirements.
- Each post has a unique title, description, and URL.
- Each post can have one or more tags.
- Each post has its publisher's name and total number of people.
- Each post has a user-given comment along with their name, message, data time and preferences.
- Each post can have 0 or more comments.
In the RDBMS architecture, the above requirements will be designed with a minimum of three tables. The relationship between a table and a table is as follows-
In MongoDB mode, the design will have a collection with the post
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 } ]}
Json
As you can see from the example above, when displaying data, you need to connect three tables in the RDBMS, and in MongoDB, the data will only appear in one collection.
The difference between MongoDB and MySQL traditional relational database