KEMBAR78
MongoDB.pptx
Sigit Kurniawan, M.Kom
MongoDB
Contents
• SQL vs NoSQL
• NoSQL Features & Types
• What is MongoDB?
• MongoDB Features
• MongoDB Data Types
• Installation MongoDB on Windows
• MongoDB CRUD Operations
• Node.js with MongoDB
SQL vs NoSQL
SQL VS NoSQL
• NoSQL (often interpreted as Not only SQL) database
• It provides a mechanism for storage and retrieval of data that is modeled
in means other than the tabular relations used in relational databases.
SQL NoSQL
Relational Database Management System (RDBMS) Non-relational or distributed database system.
These databases have fixed or static or predefined schema They have dynamic schema
These databases are best suited for complex queries These databases are not so good for complex queries
Vertically Scalable Horizontally scalable
Follows ACID property Follows BASE property
SQL VS NoSQL
• ACID Properties:
1. Atomicity; The entire transaction
takes place at once or doesn't
happen at all
2. Consistency; The database is
consistent before and after the
transaction
3. Isolation; Multiple transactions
occur independently without
interference
4. Durability; The changes of
successful transaction occurs even if
the systems failure occurs
Relational Databases
Durability
Isolation
Atomicity Consistency
ACID
SQL VS NoSQL
• CAP Properties:
1. Consistency; that the nodes will have
the same copies of a replicated data
item visible for various transactions
2. Availability; that each read or write
request for a data item will either be
processed successfully or will receive a
message that the operation cannot be
completed
3. Partition tolerance; the system can
continue operating even if the
network connecting the nodes has a
fault that results in two or more
partitions, where the nodes in each
partition can only communicate
among each other.
NoSQL Databases
Consistency
Availability
Partition tolerance
CAP
NoSQL Features & Types
NoSQL Features & Types
Fexible Schemas
Horizotal scaling
Fast queries due
to the data model
Ease of use for
developers
FEATURES
NoSQL Features & Types
Fexible Schemas
Horizotal scaling
Fast queries due
to the data model
Ease of use for
developers
FEATURES
Document
databases
Wide-column
stores
Key-value
databases
Graph databases
TYPES
What is MongoDB?
What is MongoDB ?
A database management
system designed to
rapidly develop web
applications and internet
infrastructure.
A general-purpose
document database
designed for modern
application development
and for the cloud.
A good NoSQL document
database with a range of
features that, in the
open-source NoSQL
world, are hard to beat.
What is MongoDB ?
A database management
system designed to
rapidly develop web
applications and internet
infrastructure.
A general-purpose
document database
designed for modern
application development
and for the cloud.
A good NoSQL document
database with a range of
features that, in the
open-source NoSQL
world, are hard to beat.
Document-Oriented,
No Sequel (NoSQL)
What is MongoDB ?
Database Collections
MongoDB stores data
as JSON/BSON (Binary
JSON) documents
In MongoDB, a
collection is a group of
documents
MongoDB Features
MongoDB Features
High
Performance
Auto
Replication
Horizontal
Scalability
Load
Balancing
High
Availability
Indexing
MongoDB Data Types
MongoDB Data Types
• String
This is the most commonly used datatype to store the data. String in MongoDB must be
UTF-8 valid.
• Integer
This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending
upon your server.
• Double
This type is used to store floating point values.
• Date
This datatype is used to store the current date or time in UNIX time format. You can
specify your own date time by creating object of Date and passing day, month, year
into it.
• Boolean
This type is used to store a boolean (true/ false) value.
MongoDB Data Types
• Object ID
This datatype is used to store the document’s ID.
• Array
This type is used to store arrays or list or multiple values into one key.
• Timestamp
ctimestamp. This can be handy for recording when a document has been modified or
added.
• Code
This datatype is used to store JavaScript code into the document.
• Regular Expression
This datatype is used to store regular expression.
Installation MongoDB on Windows
Installation on Windows
• Step 1: Go to MongoDB download Page
and click download as shown in the
screenshot. A .msi file like this
mongodb-win32-x86_64-2008plus-ssl-
3.4.7-signed will be downloaded in
your system. Double click on the file to
run the installer.
Installation on Windows
• Step 2: Click Run when the MongoDB
installation windows pops up.
Installation on Windows
• Step 3: Click Next when the MongoDB
installation windows pops up.
Installation on Windows
• Step 4: Accept the MongoDB user
Agreement and click Next.
Installation on Windows
• Step 5: When the setup asks you to
choose the Setup type, choose
Complete.
Installation on Windows
• Step 6: Click Next when the MongoDB
service configuration. MongoDB will be
installed in the windows service
Installation on Windows
• Step 7: Choose Install MongoDB
Compass if you want to use the
MongoDB User Interface, or ignore it
and continue by clicking Next in the
Install MongoDB Compass window
Installation on Windows
• Step 8: Click Install to begin the
installation.
MongoDB CRUD Operations
MongoDB CRUD
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key
Primary Key
(Default key _id provided by MongoDB itself)
MongoDB CRUD
CREATE DATABASE
MongoDB use DATABASE_NAME is used to create database. The command will create a
new database if it doesn't exist, otherwise it will return the existing database.
SQL Create Database
• SQL Server
CREATE DATABASE databasename;
Example: CREATE DATABASE testdb;
• MySQL
CREATE DATABASE databasename;
Example: CREATE DATABASE testdb;
MongoDB CRUD
INSERT DOCUMENT
Single Insert Multiple Insert
SQL Insert Query
• SQL Server
 Single
INSERT INTO tablename (c1,c2,...)
VALUES (v11,v12,...);
 Multiple
INSERT INTO tablename (c1,c2,...)
VALUES (v11,v12,...), (v21,v22,...), ….. , (vnn,vn2,...);
• MySQL
 Single
INSERT INTO tablename (c1,c2,...)
VALUES (v11,v12,...);
 Multiple
INSERT INTO tablename (c1,c2,...)
VALUES (v11,v12,...), (v21,v22,...), ….. , (vnn,vn2,...);
MongoDB CRUD
INSERT DOCUMENT - SINGLE
MongoDB CRUD
INSERT DOCUMENT - MULTIPLE
MongoDB CRUD
QUERY DOCUMENTS
Select All Documents
Specify Equality
Specify Conditions Using Query Operators
SELECT * FROM tablename;
SELECT * FROM tablename WHERE column;
SELECT * FROM tablename WHERE columnname IN ;
MongoDB CRUD
SELECT ALL DOCUMENTS
MongoDB CRUD
SPECIFY EQUALITY
MongoDB CRUD
SPECIFY CONDITIONS USING
QUERY OPERATORS
MongoDB CRUD
QUERY DOCUMENTS
Specify “AND” Conditions
Specify “OR” Conditions
Specify “AND” as well as “OR” Conditions
MongoDB CRUD
SPECIFY “AND” CONDITIONS
MongoDB CRUD
SPECIFY “OR” CONDITIONS
MongoDB CRUD
SPECIFY “AND” AS WELL AS “OR” CONDITIONS
MongoDB CRUD
UPDATES DOCUMENTS
Single Update Updates a single
document within the
collection based on
the filter.
SQL Update Query
• SQL Server
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
• MySQL
UPDATE table_name
SET
column_name1 = expr1,
column_name2 = expr2,
...
WHERE condition;
MongoDB CRUD
UPDATES DOCUMENTS
MongoDB CRUD
UPDATES DOCUMENTS
Multiple Update
Updates all documents
that match the specified
filter for a collection.
MongoDB CRUD
UPDATES DOCUMENTS
MongoDB CRUD
UPDATES DOCUMENTS
Replace One
Replaces a single
document within the
collection based on the
filter.
MongoDB CRUD
UPDATES DOCUMENTS
MongoDB CRUD
DELETE DOCUMENTS
Single Delete
SQL Delete Query
• SQL Server
DELETE FROM table_name WHERE condition;
• MySQL
DELETE FROM table_name WHERE condition;
MongoDB CRUD
DELETE DOCUMENTS
MongoDB CRUD
DELETE DOCUMENTS
Multiple Delete
MongoDB CRUD
DELETE DOCUMENTS
Node.js with MongoDB
MongoDB.pptx

MongoDB.pptx

  • 1.
  • 2.
    Contents • SQL vsNoSQL • NoSQL Features & Types • What is MongoDB? • MongoDB Features • MongoDB Data Types • Installation MongoDB on Windows • MongoDB CRUD Operations • Node.js with MongoDB
  • 3.
  • 4.
    SQL VS NoSQL •NoSQL (often interpreted as Not only SQL) database • It provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. SQL NoSQL Relational Database Management System (RDBMS) Non-relational or distributed database system. These databases have fixed or static or predefined schema They have dynamic schema These databases are best suited for complex queries These databases are not so good for complex queries Vertically Scalable Horizontally scalable Follows ACID property Follows BASE property
  • 5.
    SQL VS NoSQL •ACID Properties: 1. Atomicity; The entire transaction takes place at once or doesn't happen at all 2. Consistency; The database is consistent before and after the transaction 3. Isolation; Multiple transactions occur independently without interference 4. Durability; The changes of successful transaction occurs even if the systems failure occurs Relational Databases Durability Isolation Atomicity Consistency ACID
  • 6.
    SQL VS NoSQL •CAP Properties: 1. Consistency; that the nodes will have the same copies of a replicated data item visible for various transactions 2. Availability; that each read or write request for a data item will either be processed successfully or will receive a message that the operation cannot be completed 3. Partition tolerance; the system can continue operating even if the network connecting the nodes has a fault that results in two or more partitions, where the nodes in each partition can only communicate among each other. NoSQL Databases Consistency Availability Partition tolerance CAP
  • 7.
  • 8.
    NoSQL Features &Types Fexible Schemas Horizotal scaling Fast queries due to the data model Ease of use for developers FEATURES
  • 9.
    NoSQL Features &Types Fexible Schemas Horizotal scaling Fast queries due to the data model Ease of use for developers FEATURES Document databases Wide-column stores Key-value databases Graph databases TYPES
  • 10.
  • 11.
    What is MongoDB? A database management system designed to rapidly develop web applications and internet infrastructure. A general-purpose document database designed for modern application development and for the cloud. A good NoSQL document database with a range of features that, in the open-source NoSQL world, are hard to beat.
  • 12.
    What is MongoDB? A database management system designed to rapidly develop web applications and internet infrastructure. A general-purpose document database designed for modern application development and for the cloud. A good NoSQL document database with a range of features that, in the open-source NoSQL world, are hard to beat. Document-Oriented, No Sequel (NoSQL)
  • 13.
    What is MongoDB? Database Collections MongoDB stores data as JSON/BSON (Binary JSON) documents In MongoDB, a collection is a group of documents
  • 14.
  • 15.
  • 16.
  • 17.
    MongoDB Data Types •String This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. • Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. • Double This type is used to store floating point values. • Date This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it. • Boolean This type is used to store a boolean (true/ false) value.
  • 18.
    MongoDB Data Types •Object ID This datatype is used to store the document’s ID. • Array This type is used to store arrays or list or multiple values into one key. • Timestamp ctimestamp. This can be handy for recording when a document has been modified or added. • Code This datatype is used to store JavaScript code into the document. • Regular Expression This datatype is used to store regular expression.
  • 19.
  • 20.
    Installation on Windows •Step 1: Go to MongoDB download Page and click download as shown in the screenshot. A .msi file like this mongodb-win32-x86_64-2008plus-ssl- 3.4.7-signed will be downloaded in your system. Double click on the file to run the installer.
  • 21.
    Installation on Windows •Step 2: Click Run when the MongoDB installation windows pops up.
  • 22.
    Installation on Windows •Step 3: Click Next when the MongoDB installation windows pops up.
  • 23.
    Installation on Windows •Step 4: Accept the MongoDB user Agreement and click Next.
  • 24.
    Installation on Windows •Step 5: When the setup asks you to choose the Setup type, choose Complete.
  • 25.
    Installation on Windows •Step 6: Click Next when the MongoDB service configuration. MongoDB will be installed in the windows service
  • 26.
    Installation on Windows •Step 7: Choose Install MongoDB Compass if you want to use the MongoDB User Interface, or ignore it and continue by clicking Next in the Install MongoDB Compass window
  • 27.
    Installation on Windows •Step 8: Click Install to begin the installation.
  • 28.
  • 29.
    MongoDB CRUD RDBMS MongoDB DatabaseDatabase Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by MongoDB itself)
  • 30.
    MongoDB CRUD CREATE DATABASE MongoDBuse DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database.
  • 31.
    SQL Create Database •SQL Server CREATE DATABASE databasename; Example: CREATE DATABASE testdb; • MySQL CREATE DATABASE databasename; Example: CREATE DATABASE testdb;
  • 32.
  • 33.
    SQL Insert Query •SQL Server  Single INSERT INTO tablename (c1,c2,...) VALUES (v11,v12,...);  Multiple INSERT INTO tablename (c1,c2,...) VALUES (v11,v12,...), (v21,v22,...), ….. , (vnn,vn2,...); • MySQL  Single INSERT INTO tablename (c1,c2,...) VALUES (v11,v12,...);  Multiple INSERT INTO tablename (c1,c2,...) VALUES (v11,v12,...), (v21,v22,...), ….. , (vnn,vn2,...);
  • 34.
  • 35.
  • 36.
    MongoDB CRUD QUERY DOCUMENTS SelectAll Documents Specify Equality Specify Conditions Using Query Operators SELECT * FROM tablename; SELECT * FROM tablename WHERE column; SELECT * FROM tablename WHERE columnname IN ;
  • 37.
  • 38.
  • 39.
    MongoDB CRUD SPECIFY CONDITIONSUSING QUERY OPERATORS
  • 40.
    MongoDB CRUD QUERY DOCUMENTS Specify“AND” Conditions Specify “OR” Conditions Specify “AND” as well as “OR” Conditions
  • 41.
  • 42.
  • 43.
    MongoDB CRUD SPECIFY “AND”AS WELL AS “OR” CONDITIONS
  • 44.
    MongoDB CRUD UPDATES DOCUMENTS SingleUpdate Updates a single document within the collection based on the filter.
  • 45.
    SQL Update Query •SQL Server UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; • MySQL UPDATE table_name SET column_name1 = expr1, column_name2 = expr2, ... WHERE condition;
  • 46.
  • 47.
    MongoDB CRUD UPDATES DOCUMENTS MultipleUpdate Updates all documents that match the specified filter for a collection.
  • 48.
  • 49.
    MongoDB CRUD UPDATES DOCUMENTS ReplaceOne Replaces a single document within the collection based on the filter.
  • 50.
  • 51.
  • 52.
    SQL Delete Query •SQL Server DELETE FROM table_name WHERE condition; • MySQL DELETE FROM table_name WHERE condition;
  • 53.
  • 54.
  • 55.
  • 56.

Editor's Notes

  • #16 MongoDB memberikan kinerja tinggi (High Performance). Sebagian besar operasi di MongoDB lebih cepat dibandingkan dengan database relasional. MongoDB menyediakan fitur replikasi otomatis (Auto Replication) yang memungkinkan Anda memulihkan data dengan cepat jika terjadi kegagalan. Penskalaan horizontal (Horizontal Scalability) dimungkinkan di MongoDB karena berbagi. Sharding adalah mempartisi data dan menempatkannya di beberapa mesin sedemikian rupa sehingga urutan data dipertahankan. Penskalaan horizontal vs penskalaan vertikal: Penskalaan vertikal berarti menambahkan lebih banyak sumber daya ke mesin yang ada sementara penskalaan horizontal berarti menambahkan lebih banyak mesin untuk menangani data. Penskalaan vertikal tidak mudah diimplementasikan, di sisi lain penskalaan horizontal mudah diimplementasikan. Contoh basis data penskalaan horizontal: MongoDB, Cassandra dll. Penyeimbangan beban (Load Balancing): Penskalaan horizontal memungkinkan MongoDB untuk menyeimbangkan beban. Ketersediaan Tinggi (High Avalaibility): Replikasi Otomatis meningkatkan ketersediaan database MongoDB. Pengindeksan (Indexing): Indeks adalah bidang tunggal dalam dokumen. Indeks digunakan untuk menemukan data dengan cepat tanpa harus mencari setiap dokumen dalam database MongoDB. Ini meningkatkan kinerja operasi yang dilakukan pada database MongoDB.