How does MongoDB store data?
MongoDB stores data in the form of BSON -Binary encoded JSON documents which supports a huge rich collection of types. Namely Fields in BSON documents may hold arrays of values or embedded documents as requirement. In MongoDB the data base construct in such a way as a group of related collections. Each database has a distinct and informative set of data files and can contain a large number of collections. A single MongoDB deployment consists of many databases.
Watch this MongoDB video
What is a ‘document’ in MongoDB?
A record in MongoDB is a document which is been described below and a data structure composed of field and value pairs. MongoDB documents are very similar to JSON objects. The values of fields may consist of other such as documents, arrays, and arrays of documents. This is an important differentiation and vital feature devised from RDBMS systems where each field must contain only one value.
Data stored in MongoDB has a flexible schema. Documents are fetched in the same collection. They don’t require same set of fields or structure, common fields in a collection’s documents which consist of different types of data.
Some considerations while designing Schema in MongoDB
- Design your schema according to user requirements.
- Combine objects into single document if it is required to use together. Otherwise separate them without using joins.
- Duplicate the data but only limited stuffs because disk space is cheaper compared to compute time.
- Usage of joins implemented while write operation, not used on read.
- Optimize your schema for most frequent use cases.
- Complex aggregation is encouraged in the schema.
Consider the example
A client needs a database design for his blog or website creation and then notice the differences between RDBMS and MongoDB schema design.
- Every post has the unique title, description and URL as per the requirement.
- Every post can have more than one tags.
- Every post has the name of its publisher and total number of likes given per post.
- Every post has respective comments given by users along with their name, message, data-time and likes given for post.
- On each post, there can be zero or any more comments.
- In RDBMS schema, design for above requirements will be consisting of minimum three tables.