1664473609-Unit 5 - Database Management - MongoDB
1664473609-Unit 5 - Database Management - MongoDB
MongoDB
Objective
• What is MongoDB
• What is Document based storage?
• Key Features of MongoDB
• Organizations that use MongoDB
• MongoDB: CAP approach
• CRUD operations
• MongoDB vs SQL Databases
• Aggregated & Map reduce functionality
• Indexing in MongoDB
• Replication of data
• Consistency of data
What is MongoDB?
• MongoDB is a NoSQL database which stores the data in form of key-value
pairs. It is an Open Source, Document Database which provides high
performance and scalability along with data modelling and data management
of huge sets of data in an enterprise application.
• MongoDB also provides the feature of Auto-Scaling. Since MongoDB is a cross
platform database and can be installed across different platforms like Windows,
Linux etc.
What is Document based storage?
• A Document is nothing but a data structure with name-value pairs like in JSON. It
is very easy to map any custom Object of any programming language with a
MongoDB Document. For example : Student object has attributes name, roll
no and subjects, where subjects is a List.
• Document for Student in MongoDB will be like:
{
name : "Stduytonight",
rollno : 1,
subjects : ["C Language", "C++", "Core Java"]
}
Key Features of MongoDB
• Apart from most of the NoSQL default features, MongoDB does bring in some more,
very important and useful features :
MongoDB provides high performance. Input/Output operations are lesser than relational databases due to
support of embedded documents(data models) and Select queries are also faster as Indexes in MongoDB
supports faster queries.
MongoDB has a rich Query Language, supporting all the major CRUD operations. The Query Language
also provides good Text Search and Aggregation features.
Auto Replication feature of MongoDB leads to High Availability. It provides an automatic failover
mechanism, as data is restored through backup(replica) copy if server fails.
Key Features of MongoDB
• Apart from most of the NoSQL default features, MongoDB does bring in some more,
very important and useful features :
MongoDB supports multiple Storage Engines. When we save data in form of documents(NoSQL) or
tables(RDBMS) who saves the data? It's the Storage Engine. Storage Engines manages how data
is saved in memory and on disk.
Organizations that use MongoDB
• Below are some of the big and notable organizations which are using
MongoDB as database for most of their business applications.
Overview of MongoDB
• MongoDB consists of a set of databases. Each database again consists of
Collections. Data in MongoDB is stored in collections. The below figure
depicts the typical database structure in MongoDB.
MongoDB
Collections
Document
Database in MongoDB
• Database in MongoDB is nothing but a container for collections. We will
learn how to create a new Database, drop a Database and how to use an
existing Database in the coming lessons.
Collections in MongoDB
• Collection is nothing but a set of MongoDB documents. These documents
are equivalent to the row of data in tables in RDBMS. But collections in
MongoDB do not relate to any set schema as compared to RDBMS.
Collections are a way of storing related data. Being schemaless, any type
of Document can be saved in a collection, although similarity is
recommended for index efficiency. Documents can have a maximum size
of 4MB.
Document in MongoDB
• Document in MongoDB is nothing but the set of key-value pairs. These
documents will have dynamic schema which means that the documents in
the same collection do not need to possess the same set of fields.
MongoDB: CAP approach
C
Focus on Consistency and Partition tolerance
• Consistency
• all replicas contain the same version of the data
• Availability
• system remains operational on failing nodes
• Partition tolerance A P
• multiple entry points
• system remains operational on system split
CRUD operations
CRUD operations create, read, 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.
• Create
• db.collection.insert( )
• db.collection.save( )
• db.collection.update( , , { upsert: true } )
CRUD operations
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:
• Read
• db.collection.find( , )
• db.collection.findOne( , )
CRUD operations
Update Operations