Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

MongoDB

Presented by : Josmi Agnes Jose


Roll Number : 20BDA27
TABLE OF CONTENTS

What is MongoDB Comparison with


relational databses

Architecture
Features

Advantages and
Syntaxes Disadvantages

Who all uses MongoDB.


What is MongoDB
MongoDB is a document –based, open-source database.

It is a leading NoSQL database.

MongoDB database is written mainly in C++.

MongoDB is a document database, which means it stores data in


JSON-like documents.

Being a NoSQL tool means that it does not use the usual rows and
columns that you so much associate with the relational database
management.
What is MongoDB

MongoDB was founded in 2007 by Dwight Merriman, Eliot


Horowitz and Kevin Ryan. It was released in 2009.

It was developed by a New-York based organization,


10gen,now called MongoDB Inc.

“Mongo” is the short for the word humongous.


Document Database

MongoDB stores data as documents.

It stores the data in field and value pairs.

MongoDB uses BSON format to store the data into a document.

Documents written in JSON format is readable to humans, so it


is easy to understand.

We can generate embedded documents and arrays that reduce


the need for Joins.
Architecture
Replication

Replication is a process of, distributing the data on multiple


servers and keeping everything synchronized.

We need to have a replica set of atleast 3 members by default


(max 50).

Every 2 sec, replica set members sends a heartbeat to each


other. If heartbeat is not received within 10 sec ,its marked as a
failure.

By default ,only primary accepts writes from client.


The secondary members copy & apply the operations in the
oplog (in primary) to their dataset asynchronously.

Disaster recovery and High availability.


Architecture
Sharding

It is the process of writing data across different servers to distribute


the read and write load and data storage requirements.

With sharding, you add more machines to support data growth and
the demands of read and write operations.

Horizontal Scaling.

We don’t shard a database.we shard a collection based on a shard


key. The default chunk size in MongoDB is 64 megabytes.

A single shard is usually composed of a replica set.


Architecture
MongoDB Cluster contains three separate components

1. Config Server (mongod)


Config servers are used to store the metadata that links requested data with the shard that
contains it. It organizes the data so that information can be retrieved reliably and consistently.

2. Query Routers (mongos)


These machines are responsible for communicating to the config servers to figure out where
the requested data is stored. For a sharded cluster, the mongos instances provide the interface between the
client applications and the sharded cluster. The mongos instances route queries and write operations to the
shards.

3. Shard Servers (mongod)


Shards are responsible for the actual data storage operations. In production environments, a
single shard is usually composed of a replica set instead of a single machine.
Architecture
Basic Queries in MongoDB
Query Purpose
db.version() Displays the version that is being used.
db.stats() returns a document with statistics reflecting the database
system’s state.
db.help() Text output listing common methods on the db object.

db To display the database you are using.


use <database> To create a new database or to switch to an exixting one
show dbs To list the databases available to the user.
db.createCollection(<collection_name>) To create new collection.
show collections To display existing collections in a database.
db.dropDatabase() To drop a existing database.

db.<collection_name>.drop() To drop a collection from the database.


CRUD Operations

Create or insert operations add new documents to


Create or Insert operations a collection. If the collection does not currently
exist, insert operations will create the collection.

db.collection.insertOne()

db.collection.insert()

db.collection.insertMany()
CRUD Operations

Read operations retrieve documents from a


Read operations collection; i.e. query a collection for documents.
we can specify query filters or criteria that identify
the documents to return.

db.collection.find() db.collection.find().pretty()

db.collection.findone() db.collection.find().count() db.collection.find().limit()


CRUD Operations

Update operations modify existing documents in a


Update operations collection. You can specify criteria, or filters, that
identify the documents to update.MongoDB
provides the following methods to update
documents of a collection:

db.collection.update()

db.collection.replaceOne() db.collection.updateMany()
CRUD Operations

Delete operations remove documents from a


Delete operations collection. You can specify criteria, or filters, that
identify the documents to remove.

db.collection.deleteOne() db.collection.deleteMany({})

db.collection.remove()
How is MongoDB different ?
Relational Databases MongoDB

Supports SQL query language Supports JSON query language

Table based Collection based and key-value pair

Row based Document based

Column based Field based

Support foreign key No support for foreign key

Contains schema which is predefined Contains dynamic schema


How is MongoDB different ?
Relational Databases MongoDB

Not fit for hierarchical data storage Best fit for hierarchical data storage

Supports JOIN statements. Does not support JOIN statements.

Vertically scalable - increasing RAM, 100 Horizontally scalable - add more servers
times faster
Emphasizes on ACID properties (Atomicity, Emphasizes on CAP theorem (Consistency,
Consistency, Isolation and Durability) Availability and Partition tolerance)
A great choice if you have structured data. A great choice if you have unstructured and
structured data with the potential for rapid
growth.
Slower than MongoDB. Faster than MySQL.
Features

MongoDB supports queries that were not known


Ad –hoc Queries while establishing a structure for the database.
Ad hoc queries function in a way to better the
performance and are real-time.

In MongoDB, you can search by field, range


query and it also supports regular expression
searches

We can index any field in MongoDB. Creating


Indexing indexes helps in faster search results. Indexing is
possible with any field or key, in a document.
This points to high availability and scalability. It has
High Performance faster query response because of indexing and
replication. This makes it a better choice for big data
and real-time applications.

MongoDB makes use of collections and


Document Oriented documents. Documents consist of key-value
pairs which are the basic unit of data in
MongoDB.

Schema-Less Database It is a schema-less database written in C++.


MongoDB supports many languages including C, C++,
Supports Many Languages Ruby, C#, Perl, PHP, Python, Java, JavaScript,
Erlang, and Haskell.

MongoDB supports un-structured or semi-


structured data.
Features

Aggregation

Aggregation operations process data records and


return computed results.

Aggregation operations group values from multiple


documents together, and can perform a variety of
operations on the grouped data to return a single
result.

MongoDB provides three ways to perform


aggregation: the aggregation pipeline, the map-
reduce function, and single purpose aggregation
methods.
GridFS

GridFS is a MongoDB API for storing and retrieving files that


exceed the BSON-document size limit of 16 MB.

Instead of storing a file in a single document , GridFS divides the


file into parts, or chunks , and stores each chunk as a separate
document. By default , GridFS divides a file into chunks of 255
kB with the exception of the last chunk.

GridFS uses two collections to store files (fs.files and fs.chunks


). One collection stores the file chunks, and the other stores file
metadata.
Advantages
 We do not need to design the schema of the database when we are using MongoDB.
This saves time. Thus, the code we write defines the schema.

 No complex joins are needed in MongoDB. There is no relationship among data in


MongoDB.

 MongoDB is easy to scale.

 It is very easy to set-up and install MongoDB.


Advantages

 Because the MongoDB uses JSON format to store data, it is very easy to store
arrays and objects.

 MongoDB is free to use. There is no cost for it.

 Performance of MongoDB is much higher than compared to any relational


database.

 The document query language supported by MongoDB is very simple as compared


to SQL queries.
Disadvantages

 MongoDB uses high memory for data storage.

 There is a limit for document size, i.e. 16mb.

 The relationships in MongoDB are not typically well-defined and the resulting
duplicate data sets can be hard to handle.

 Joins not supported.


Who uses MongoDB
REFERENCES
 MongoDB :- https://docs.mongodb.com/manual/ (Manual)

 Sharding :- https://www.youtube.com/watch?v=ooF021_Kbck&list=LL&index=3
https://www.youtube.com/watch?v=5faMjKuB9bc&list=LL&index=1
https://www.youtube.com/watch?v=4upppuW7lGE&list=LL&index=6

 Indexing :- https://www.youtube.com/watch?v=vj_YkHxzF2s&list=LL&index=8

 Queries :- https://www.youtube.com/watch?v=ZrEu5H-iFe0&t=1914s
https://www.youtube.com/watch?v=kWxEghkwpsE

 Replication :- https://docs.mongodb.com/manual/replication/

 Installation :- https://www.c-sharpcorner.com/UploadFile/f0b2ed/mondodb-day-2-install-mondodb-in-window/
THANKS!

Do you have any questions ?

You might also like