Mongo DB
Mongo DB
MongoDB vs RDBMS:
We can directly compare the MongoDB NoSQL with the RDBMS and map the varied
terminologies in the two systems: The RDBMS table is a MongoDB collection, the column is
a field, the tuple/row is a document, and the table join is an embedded document. The typical
schema of a relational database shows the number of tables and the relationship between the
tables, but MongoDB does not follow the concept of relationship.
MongoDB differs from RDBMS. This blog has elucidated nine different comparisons
between the two.
MongoDB RDBMS
Document-oriented and non-relational
Relational database
database
Document-based Row-based
Gives JavaScript client for querying Doesn’t give JavaScript for querying
Has dynamic schema and ideal for Has predefined schema and not good for
hierarchical data storage hierarchical data storage
MongoDB vs MySQL
The following table explains the differences between MongoDB and MySQL
MongoDB MySQL
The query language is javascript The query language is a structured query language
Defining the schema is not required Defining tables and columns is required
MongoDB was built with high Although the MySQL idea does not allow for
availability and scalability in mind and effective replication and sharding, it does let users
offers replication and sharding out of the retrieve related data via joins, which reduces
box. duplication.
MongoDB Architecture
MongoDB employs the single-master architecture meaning there is a
primary machine taking charge of all client-side write operations. All other
instances you add later to the cluster constitute the secondary nodes, which
commonly handle all the read operations.
MongoDB achieves replication using the concept replica sets. A replica
set is a group of mongod instances that host the same data set. One of the nodes
is selected as the primary or main node. This is called the primary node. All
others are called secondary nodes. The primary node receives all the operations
from the user and the secondaries are updated from the primary one by using the
same operation to maintain consistency.
If the primary node goes down, one of the secondary nodes is selected as the
primary node and the operations are carried forward. When the fallen node
recovers, it joins the cluster as the secondary nodes. We can control our cluster
of mongo instances using Mongo Atlas.
Mongo clusters are created based on the idea of horizontal scaling and adding of instances.
Sharding in Mongo DB
Sharding is used by MongoDB to store data across multiple machines. It uses
horizontal scaling to add more machines to distribute data and operation with respect to the
growth of load and demand.
Sharding arrangement in MongoDB has mainly three components:
Shards or replica sets: Each shard serves as a separate replica set. They store all the data.
They target to increase the consistency and availability of the data.
Configuration Servers: They are like the managers of the clusters. These servers contain the
cluster's metadata. They actually have the mapping of the cluster's data to the shards. When a
query comes, query routers use these mappings from the config servers to target the required
shard.
Query Router: The query router is mongo instances which serve as interfaces for user
applications. They take in the user queries from the applications and serve the applications
with the required results. Usually, there are multiple query router per cluster for load
distribution.
Features of MongoDB
In MongoDB, you can search by field, range query and it also supports regular expression
searches.
2. Indexing
3. Replication
A master can perform Reads and Writes and a Slave copies data from the master and can only
be used for reads or back up (not writes)
4. Duplication of data
MongoDB can run over multiple servers. The data is duplicated to keep the system up and
also keep its running condition in case of hardware failure.
5. Load balancing
10. Stores files of any size easily without complicating your stack.
The main challenge in data modelling is balancing the need of the application, the
performance characteristics of the database engine, and the data retrieval patterns.
MongoDB provides two types of data models: — Embedded data model and Normalized
data model. Based on the requirement, you can use either of the models while preparing doc.
Effective data models support our application needs in Mongo DB. The key consideration
for the structure of our documents is the decision to embed or to use references.
Compatibility
We can use the following data models for deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Atlas is a multi-cloud database service by the same people that build
MongoDB. Atlas simplifies deploying and managing your databases while offering the
versatility you need to build resilient and performant global applications on the cloud
providers of your choice.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Enterprise is available for MongoDB Enterprise subscribers and includes several
additional features including LDAP authentication, Kerberos authentication, and System
Event Auditing.
MongoDB Community: The source-available, free-to-use, and self-managed version of
MongoDB
{
_id: ,
Emp_ID: "10025AE336"
Personal_details:{
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26"
},
Contact: {
e-mail: "radhika_sharma.123@gmail.com",
phone: "9848022338"
},
Address: {
city: "Hyderabad",
Area: "Madapur",
State: "Telangana"
}
}
Embedded data models allow applications to store related pieces of information in the same
database record. As a result, applications may need to issue fewer queries and updates to
complete common operations.
In general, use embedded data models when:
We have "contains" relationships between entities. Model One-to-One Relationships with
Embedded Documents.
we have one-to-many relationships between entities. In these relationships the "many" or
child documents always appear with or are viewed in the context of the "one" or parent
documents. See Model One-to-Many Relationships with Embedded Documents.
In general, embedding provides better performance for read operations, as well as the ability
to request and retrieve related data in a single database operation. Embedded data models
make it possible to update related data in a single atomic write operation.
To access data within embedded documents, use dot notation to "reach into" the embedded
documents. See query for data in arrays and query data in embedded documents for more
examples on accessing data in arrays and embedded documents.
Embedded Data Model and Document Size Limit
Documents in MongoDB must be smaller than the maximum BSON document size.
For bulk binary data, consider GridFS.
While in MongoDB schema, design will have one collection post and 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 showing the data, in RDBMS you need to join three tables and in MongoDB, data
will be shown from one collection only.
Kickstart Your
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
To learn more about performing CRUD operations in the UI for deployments hosted in
MongoDB Atlas, see Create, View, Update, and Delete Documents.
Create Operations
Create or insert operations add new documents to a collection. If the collection does not
currently exist, insert operations will create the collection.
In MongoDB, insert operations target a single collection. All write operations in MongoDB
are atomic on the level of a single document.
Read Operations
Read operations retrieve documents from a collection; i.e. query a collection for documents.
MongoDB provides the following methods to read documents from a collection:
db.collection.find()
You can specify query filters or criteria that identify the documents to return.
click to enlarge
Query Documents
Query an Array
Update Operations
In MongoDB, update operations target a single collection. All write operations in MongoDB
are atomic on the level of a single document.
You can specify criteria, or filters, that identify the documents to update. These filters use the
same syntax as read operations.
Delete Operations
Delete operations remove documents from a collection. MongoDB provides the following
methods to delete documents of a collection:
In MongoDB, delete operations target a single collection. All write operations in MongoDB
are atomic on the level of a single document.
You can specify criteria, or filters, that identify the documents to remove. These filters use
the same syntax as read operations.