0% found this document useful (0 votes)
115 views11 pages

MongoDB Architecture and Features Overview

MongoDB is a NoSQL document-oriented database that stores data in BSON-like documents within collections, offering high performance, availability, and horizontal scalability through sharding. It supports various features such as dynamic schemas, embedded documents, and a rich query API, while also providing replication for data redundancy. The architecture includes drivers for application communication, a storage engine for performance, and a shell for database interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views11 pages

MongoDB Architecture and Features Overview

MongoDB is a NoSQL document-oriented database that stores data in BSON-like documents within collections, offering high performance, availability, and horizontal scalability through sharding. It supports various features such as dynamic schemas, embedded documents, and a rich query API, while also providing replication for data redundancy. The architecture includes drivers for application communication, a storage engine for performance, and a shell for database interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to MongoDB

Document Database
A record in MongoDB is a document, which is a data structure composed of field and value
pairs. MongoDB documents are similar to JSON objects. The values of fields may include other
documents, arrays, and arrays of documents.

The advantages of using documents are:


Documents correspond to native data types in many programming languages.
Embedded documents and arrays reduce need for expensive joins.
Dynamic schema supports fluent polymorphism.

Collections/Views/On-Demand Materialized Views


MongoDB stores documents in collections. Collections are analogous to tables in relational
databases.
In addition to collections, MongoDB supports:
Read-only Views (Starting in MongoDB 3.4)
On-Demand Materialized Views (Starting in MongoDB 4.2).

Key Features

High Performance
MongoDB provides high performance data persistence. In particular,
Support for embedded data models reduces I/O activity on database system.
Indexes support faster queries and can include keys from embedded documents and arrays.

Query API
The MongoDB Query API supports read and write operations (CRUD) as well as:
Data Aggregation
Text Search and Geospatial Queries.

High Availability
MongoDB's replication facility, called replica set, provides:
automatic failover
data redundancy.
A replica set is a group of MongoDB servers that maintain the same data set, providing
redundancy and increasing data availability.

Horizontal Scalability
MongoDB provides horizontal scalability as part of its core functionality:
Sharding distributes data across a cluster of machines.
Starting in 3.4, MongoDB supports creating zones of data based on the shard key. In a balanced
cluster, MongoDB directs reads and writes covered by a zone only to those shards inside the
zone. See the Zones manual page for more information.

Support for Multiple Storage Engines


MongoDB supports multiple storage engines:
WiredTiger Storage Engine (including support for Encryption at Rest)
In-Memory Storage Engine.

MongoDB Architecture

MongoDB is a popular NoSQL document-oriented database management system, known for its
flexibility, high performance, high availability, and multi-storage engines. The
term NoSQL means non-relational. It means that MongoDB isn’t based on a table-
like relational database structure. It is used by Adobe, Uber, IBM, and Google. In this article, we
will delve into the MongoDB architecture, exploring its key components and how they work
together.
Key Features of MongoDB
 Document-oriented Database
 Stores data in BSON-like documents.
 Schema Less database.
 It provides horizontal scalability with the help of sharding.
 It provides high availability and redundancy with the help of replication.
 It allows one to perform operations on the grouped data and get a single result or computed
result.
 It has very high performance.
MongoDB Vs RDBMS
MongoDB RDBMS

It is a non-relational, document-oriented
It is a Relational database management system
database management system and works on
and works on relational databases.
document-based database

MongoDB uses a document structure to The table structure is used to store the data in
store the data. RDBMS

It has schema-less databases. It uses the schema structure.

Supports MongoDB Query Language (MQL) Supports SQL query language.


MongoDB RDBMS

of BSON type

It follows the CAP theorem. It follows ACID properties.

In MongoDB, the database can be scaled In RDBMS, we can scale the database
horizontally. vertically.

MongoDB Architecture and its Components


MongoDB’s architecture design involves several important parts that work together to create a
strong and flexible database system. these are the following MongoDB’s architecture

1. Drivers & Storage Engine


MongoDB store the data on the server but that data we will try to retrieve from our application.
So that time how the communication is happening between our application and MongoDB
server.
Any application which is written in python, .net and java or any kind of frontend application,
these application are trying to access the data from these physical storage in server. First they
will interact with driver which will communicate with MongoDB server. What happen is once
the request is going from the frontend application through the driver then driver will change
appropriate query by using query engine and then the query will get executed in MongoDB
data model. Left side is security which provides security to the database that who will access
the data and right side is management this management will manage all these things.
Drivers
Drivers are client libraries that offer interfaces and methods for applications to communicate
with MongoDB databases. Drivers will handle the translation of documents between BSON
objects and mapping application structures.
.NET, Java, JavaScript, [Link], Python, etc are some of the widely used drives supported by
MongoDB.
Storage Engine
The storage engine significantly influences the performance of applications, serving as an
intermediary between the MongoDB database and persistent storage, typically disks.
MongoDB supports different storage engines:
 MMAPv1 – It is a traditional storage engine based on memory mapped files. This
storage engine is optimized for workloads with high volumes of read operations, insertions,
and in-place updates. It uses B-tress to store indexes. Storage Engine works on multiple
reader single writer lock. A user cannot have two write calls to be processes in parallel on
the same collection. It is fast for reads and slow for writes.
 Wired Tiger – Default Storage Engine starts from MongoDB 3version. No locking
Algorithms like hash pointer. It yields 7x-10x better write operations and 80% of the file
system compression than MMAP.
 InMemory – Instead of storing documents on disk, the engine uses in-memory for
more predictable data latencies. It uses 50% of physical RAM minimum 1 GB as default.
It requires all its data. When dealing with large datasets, the in-memory engine may not be
the most suitable choice.
2. Security
 Authentication
 Authorization
 Encryption on data
 Hardening (Ensure only trusted hosts have access)
3. MongoDB Server
It serves as the central element and is in charge of maintaining, storing, and retrieving
data from the database through a number of interfaces. The system’s heart is the MongoDB
server. Each mongod server instance is in charge of handling client requests, maintaining data
storage, and performing database operations. Several mongod instances work together to form
a cluster in a typical MongoDB setup.
4. MongoDB Shell
For dealing with MongoDB databases, MongoDB provides the MongoDB Shell command-
line interface (CLI) tool. The ability to handle and query MongoDB data straight from the
terminal is robust and flexible. After installing MongoDB, you may access the MongoDB
Shell, often known as mongo. It interacts with the database using JavaScript-based syntax.
Additionally, it has built-in help that shows details about possible commands and how to use
them.
5. Data Storage in MongoDB
5.1 Collections
A database can contain as many collections as it wishes, and MongoDB stores data inside
collections.
As an example, a database might contain three collections a user’s collection, a blog post
collection, and a comments collection. The user collection would hold user data and
documents, the blog post collection would hold blog posts and documents, and the comments
collection would hold documents related to comments. This would allow for the easy retrieval
of all the documents from a single collection.
5.2 Documents
Documents themselves represent the individual records in a specific collection.
For example inside the blog posts collection we’d store a lot of blog post documents and each
one represents a single blog post now the way that data is structured inside a document looks
very much like a JSON object with key value pairs but actually it’s being stored as something
called BSON which is just binary JSON.
6. Indexes
Indexes are data structures that make it simple to navigate across the collection’s data set. They
help to execute queries and find documents that match the query criteria without a collection
scan.
These are the following different types of indexes in MongoDB:
6.1 Single field
MongoDB can traverse the indexes either in the ascending or descending order for single-
field index
[Link]({“item”:1})
In this example, we are creating a single index on the item field and 1 here represents the filed
is in ascending order.
A compound index in MongoDB contains multiple single filed indexes separated by a comma.
MongoDB restricts the number of fields in a compound index to a maximum of 31.
[Link]({“item”: 1, “stock”:1})
Here, we create a compound index on item: 1, stock:1
6.2 Multi-Key
When indexing a filed containing an array value, MongoDB creates separate index entries
for each array component. MongoDB allows you to create multi-key indexes for arrays
containing scalar values, including strings, numbers, and nested documents.
[Link]({<filed>: <1 or -1>})
6.3 Geo Spatial
Two geospatial indexes offered by MongoDB are called 2d indexes and 2d sphere indexes.
These indexes allow us to query geospatial data. On this case, queries intended to locate data
stored on a two-dimensional plane are supported by the 2d indexes. On the other hand, queries
that are used to locate data stored in spherical geometry are supported by 2D sphere indexes.
6.4 Hashed
To maintain the entries with hashes of the values of the indexed field we use Hash Index.
MongoDB supports hash based sharding and provides hashed indexes.
db.<collection>.createIndex( { item: “hashed” } )
7. Replication
Within a MongoDB cluster, data replication entails keeping several copies of the same data on
various servers or nodes. Enhancing data availability and dependability is the main objective
of data replication. A replica may seamlessly replace a failing server in the cluster to maintain
service continuity and data integrity.
 Primary Node (Primary Replica): In a replica set, the primary node serves as the main
source for all write operations. It’s the only node that accepts write requests. The main
node is where all data modifications begin and are implemented initially.
 Secondary Nodes: Secondary nodes duplicate data from the primary node (also known as
secondary replicas). They are useful for dispersing read workloads and load balancing since
they are read-only and mostly utilized for read activities.
8. Sharding
Sharding is basically horizontal scaling of databases as compared to the traditional vertical
scaling of adding more CPUS and ram to the current system.
For example, you have huge set of files you might segregate it into smaller sets for ease.
Similarly what mongo database does is it segregates its data into smaller chunks to improve
the efficiency.
you have a machine with these configuration and mongo db instance running on it storing 100
million documents.
Now with time your data will grow in your mongo db instance and suppose 100 million extra
documents get added. Now to manage the processing of these extra records you might need to
add extra ram, extra storage and extra CPU to the server. Such type of scaling is called vertical
scaling.
Now consider another situation if you have 4 small machines with small configurations. You
can divide 200 million of document into each of the server such that each of the server might
hold around 50 million documents. By dividing the data into multiple servers you have reduced
the computation requirements and such kind of scaling is known as horizontal scaling and this
horizontal scaling is known as sharding in mongo and each of the servers S1, S2, S3, S4 are the
shards.

Using the MongoDB Shell

The MongoDB shell is a great tool for navigating, inspecting, and even manipulating document
data. If you’re running MongoDB on your local machine, firing up the shell is as simple as
typing mongo and hitting enter, which will connect to MongoDB at localhost on the standard
port (27017). If you’re connecting to a MongoDB Atlas cluster or other remote instance, then
add the connection string after the command mongo .

Here are a few quick shell examples:

List Databases

> show dbs;


admin 0.000GB
config 0.000GB
local 0.000GB
my_database 0.004GB
>

List Collections

> use my_database;


> show collections;
users
posts
>

Count Documents in a Collection

> use my_database;


> [Link]()
20234
>

Find the First Document in a Collection

> [Link]()
{
"_id": ObjectId("5ce45d7606444f199acfba1e"),
"name": {given: "Alex", family: "Smith"},
"email": "email@[Link]"
"age": 27
}
>

Find a Document by ID

> [Link]({_id: ObjectId("5ce45d7606444f199acfba1e")})


{
"_id": ObjectId("5ce45d7606444f199acfba1e"),
"name": {given: "Alex", family: "Smith"},
"email": "email@[Link]",
"age": 27
}
>

How to Create Database & Collection in MongoDB?



MongoDB stores data records as documents that are stored together in collections and the
database stores one or more collections of documents.
Document: A document is a basic unit of storing data into the database. A single record of a
collection is also known as a document. Basically, It is a structure that compromises key &
value pairs which is similar to the JSON objects. Documents have a great ability to store
complex data. For example:

Here, the name, country, age and status are fields, and gfg, India, 21, A are their values.
Collection: It is used to store a varied number of documents inside the database. As MongoDB
is a Schema-free database, it can store documents that are not the same in structure. Also, there
is no need to define the columns and their datatype.

Database: The MongoDB database is a container for collections and it can store one or more
collections. It is not necessary to create a database before you work on it. The show dbs
command gives the list of all the databases.
Creating a Database
In MongoDB, we can create a database using the use command. As shown in the below image

use gfgDB
Here, we have created a database named as “gfgDB”. If the database doesn’t exist, MongoDB
will create the database when you store any data to it. Using this command we can also switch
from on database to another.
How to view all the existing database?
In MongoDB, we can view all the existing database using the following command:
show dbs
This command returns a list of all the existing databases.

Creating a Collection
createCollection() in MongoDB – In MongoDB,collection is created using createCollection()
[Link] name is passed as argument to the method.
Syntax
[Link](‘ collection_name’ );
Example : Create Student collection in MongoDB
Query :
use GeeksforGeeks;
// To create Collection
[Link]('Student');
// To insert into the collection
[Link]({ Name:"Om",Age:19} );
//To query the collection
[Link]();

createCollection() in MongoDB

Explanation:
In the above example,GeeksforGeeks database is [Link] Collection is created using
createCollection() method..One document is inserted in the Student collection.
In MongoDB, a collection is also created directly when we add one or more documents to it.
We can insert documents in the collection using the following methods:
 Inserting only one document in the collection
[Link]( { name:"geeksforgeeks" } )
Here, we create a collection named as myNewCollection1 by inserting a document that
contains a name field with its value in it using insertOne() method.

 Inserting many documents in the collection


[Link]([{name:"gfg", country:"India"},
{name:"rahul", age:20}])
Here, we create a collection named as myNewCollection2 by inserting two documents
using insertMany() method.
How to view all the existing collections in the database?
In MongoDB, we can view all the existing collection in the database using the following
command:
show collections
This command returns a list of all the existing collections in the gfgDB database.

You might also like