Data in MongoDB contains a versatile schema. Documents that are in a same collection don't need to have an equivalent set of fields or structure, and common fields in an exceedingly collection's documents could hold differing kinds of data.
Some facts need to be consider while playing with schema in MongoDB
- Prepare your schema as per the need of project.
- mix objects into one document if you'll use them along. Otherwise separate them (but avoid to make use of JOINs).
- Duplicate the information (but limited) as a result of disc space is affordable as compare to calculate time.
- Optimize your schema to deal with frequent use cases.
- Do complicated aggregation within the schema
Example
Suppose a consumer desires a database for his blogging web site and see the variations between RDBMS and MongoDB schema style. web site has the subsequent necessities.
- each post has the distinctive title, description.
- each post will have one or multiple tags.
- each post has the ID of its publisher.
- each Post have comments given by users in conjunction with their name, message, data-time.
- On every post there will be zero or additional comments.
In RDBMS schema style for on top of necessities can have minimum 3 tables.
- For store post.
- For store comment.
- For store tags.
And you need to make relations between these tables i.e.
- Comment related to post.
- Tags related to post.
While in MongoDB schema the same implementation can be achieved using below subsequent structure:
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
category: CATEGORY_ID,
creator: CREATOR_ID,
tags: [TAG1, TAG2, TAG3],
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME
},
{
user:'USER_ID',
message: TEXT,
dateCreated: DATE_TIME
}
]
}
0 Comment(s)