UCCD2303_Practical07_slides_MongoDB
UCCD2303_Practical07_slides_MongoDB
Practical 7:
Introduction to the Tools in the
NoSQL Database – Part 1
By Mr Cheang Kah Wai, Alvin
Practical 7 Task:
Introduction to the Tools in the NoSQL Database – Part 1
|-UCCD2303/UCCD2203-| 2
Introduction to NoSQL
Database and MongoDB
NoSQL Database
◼ A broad array of nonrelational database technologies
that have developed to address the challenges
represented by Big Data.
◼ Also known as ‘Not Only SQL’.
◼ Non-tabular, and data is stored differently than
relational tables.
◼ NoSQL provide flexible schemas and scale easily with
large amounts of data and high user loads.
◼ Example: MongoDB, CouchDB, CouchBase,
Cassandra, HBase, Redis, Riak, Neo4J
|-UCCD2303/UCCD2203-| 4
NoSQL Database
◼ Types of NoSQL Database
|-UCCD2303/UCCD2203-| 5
MongoDB
◼ MongoDB is an open-source document database,
which also offers commercial support.
◼ As the name MongoDB - derived from the word
“humongous” - indicates, its main goal is to handle
huge amounts of data.
◼ While MongoDB is implemented in C++, it uses
JavaScript as a querying language.
|-UCCD2303/UCCD2203-| 6
MongoDB
◼ A non-relational database for applications.
◼ A form of storage.
◼ Highly available, scalable, fault-tolerant, agile and
flexible.
◼ Instead of storing the traditional tables and rows of
data in a data frame, it stores JSON/dictionary-like
objects.
◼ Powerful and flexible as users can store documents
within documents and theirs documents do not require
a fixed or consistent format.
|-UCCD2303/UCCD2203-| 7
MongoDB
◼ Advantages:
❑ Schema less − Number of fields, content and size of the
document can differ from one document to another.
❑ Structure of a single object is clear.
❑ No complex joins.
❑ Deep query-ability.
◼ MongoDB supports dynamic queries on documents using a document-
based query language that's nearly as powerful as SQL.
❑ Tuning.
❑ Ease of scale-out − MongoDB is easy to scale.
|-UCCD2303/UCCD2203-| 8
MongoDB
◼ Advantages (continued):
❑ Conversion/mapping of application objects to database
objects not needed.
❑ Uses internal memory for storing the (windowed) working set,
enabling faster access of data.
|-UCCD2303/UCCD2203-| 9
MongoDB
◼ Why use MongoDB?
❑ Document Oriented Storage − Data is stored in the form of
JSON style documents.
❑ Index on any attribute
❑ Replication and high availability
❑ Auto-sharding – (process of storing data records across
multiple machines).
❑ Rich queries
❑ Fast in-place updates
❑ Professional support by MongoDB
|-UCCD2303/UCCD2203-| 10
MongoDB
◼ Where to use MongoDB?
❑ Big Data
❑ Content Management and Delivery
❑ Mobile and Social Infrastructure
❑ User Data Management
❑ Data Hub
|-UCCD2303/UCCD2203-| 11
MongoDB
◼ MongoDB works on concept of collection and document.
◼ Database is a physical container for collections. Each
database gets its own set of files on the file system. A
single MongoDB server typically has multiple
databases.
|-UCCD2303/UCCD2203-| 12
MongoDB
◼ Collection is a group of MongoDB documents.
❑ It is the equivalent of an RDBMS table. A collection exists
within a single database.
❑ Collections do not enforce a schema.
❑ Documents within a collection can have different fields.
❑ Typically, all documents in a collection are of similar or
related purpose.
|-UCCD2303/UCCD2203-| 13
MongoDB
◼ Document is a set of key-value pairs.
❑ Documents have dynamic schema.
❑ Dynamic schema means that documents in the same
collection do not need to have the same set of fields or
structure, and common fields in a collection's documents may
hold different types of data.
|-UCCD2303/UCCD2203-| 14
MongoDB
|-UCCD2303/UCCD2203-| 15
MongoDB
Sample Document
|-UCCD2303/UCCD2203-| 16
MongoDB
◼ MongoDB ID
❑ _id is a 12 bytes hexadecimal number which assures the
uniqueness of every document.
❑ You can provide _id while inserting the document. If you
don’t provide, then MongoDB provides a unique id for every
document.
❑ These 12 bytes first 4 bytes for the current timestamp, next 3
bytes for machine id, next 2 bytes for process id of MongoDB
server and remaining 3 bytes are simple incremental VALUE.
|-UCCD2303/UCCD2203-| 17
Installing NoSQL
Database - MongoDB
Installing MongoDB
|-UCCD2303/UCCD2203-| 19
Installing MongoDB
|-UCCD2303/UCCD2203-| 20
Installing MongoDB
◼ Double click mongodb-windows-x86_64-4.4.6-signed.exe
|-UCCD2303/UCCD2203-| 21
Installing MongoDB
◼ Check accept agreement.
◼ Click ‘Next’ button.
|-UCCD2303/UCCD2203-| 22
Installing MongoDB
◼ Choose Setup Type as "Complete" .
|-UCCD2303/UCCD2203-| 23
Installing MongoDB
◼ Follow the wizard.
◼ Check on “Install MongoDB Compass”.
◼ Click ‘Next’ button until ‘Finish’.
|-UCCD2303/UCCD2203-| 24
Installing MongoDB
◼ After installation, you will find MongoDB server is available in C drive.
|-UCCD2303/UCCD2203-| 25
Installing MongoDB
◼ MongoDBCompass can be found in Windows Start → Program files → MongoDB
Inc → MongoDBCompass
|-UCCD2303/UCCD2203-| 26
Introduction to Mongo
Shell
Introduction to Mongo Shell
|-UCCD2303/UCCD2203-| 28
Mongo Shell
◼ Open Windows explorer and navigate to → C:\Program
Files\MongoDB\Server\4.4\bin
◼ To initiate the MongoDB server:
◼ Double click on mongod.exe
◼ To open the client to run the MongoDB commands:
◼ Double-click on mongo.exe
◼ Type this example:
var myCourse='Database Technology';
printjson(myCourse);
|-UCCD2303/UCCD2203-| 29
Mongo Shell
|-UCCD2303/UCCD2203-| 30
Mongo Shell
◼ Alternatively, using Command Prompt:
❑ Type mongod to start the MongoDB Server
|-UCCD2303/UCCD2203-| 31
Import CSV data with
mongoimport.exe
Import CSV data with mongoimport.exe
◼ Unzip mongodb-database-tools-windows-
x86_64-100.3.1.zip into a folder.
|-UCCD2303/UCCD2203-| 33
Import CSV data with mongoimport.exe
|-UCCD2303/UCCD2203-| 34
Import CSV data with mongoimport.exe
|-UCCD2303/UCCD2203-| 35
Import CSV data with mongoimport.exe
◼ Type:
cd C:\Program Files\MongoDB\Server\4.4\bin
|-UCCD2303/UCCD2203-| 36
Import CSV data with mongoimport.exe
|-UCCD2303/UCCD2203-| 37
Import CSV data with mongoimport.exe
|-UCCD2303/UCCD2203-| 38
Import CSV data manually
with MongoDB Compass,
a MongoDB GUI
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ In MongoDB Compass, connect to localhost.
Hostname: localhost
Port: 27017
◼ At the dashboard, click the “Create Database” button.
◼ Specify the Database Name as EmpD
◼ Specify the Collection Name as EmpC
◼ Click the “Create Database” button
|-UCCD2303/UCCD2203-| 40
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ Database Name: EmpD
◼ Collection Name: EmpC
|-UCCD2303/UCCD2203-| 41
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
|-UCCD2303/UCCD2203-| 42
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ Click “Import Data” button.
|-UCCD2303/UCCD2203-| 43
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ Click “BROWSE” button to locate Employee.csv
◼ Click “CSV”
|-UCCD2303/UCCD2203-| 44
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ Click “IMPORT” button at the bottom of the screen.
|-UCCD2303/UCCD2203-| 45
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ Click “DONE” button.
|-UCCD2303/UCCD2203-| 46
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ To view the data/document, click “EmpC” at the left panel.
|-UCCD2303/UCCD2203-| 47
Import CSV data manually with
MongoDB Compass, a MongoDB GUI
◼ To quit MongoDB, select “Disconnect”.
|-UCCD2303/UCCD2203-| 48
View data or collection of
documents by using
MongoDB Compass
View data or collection of documents by
using MongoDB Compass
◼ Open MongoDB Compass Community that was installed earlier.
◼ Connect to the MongoDB Server.
|-UCCD2303/UCCD2203-| 50
View data or collection of documents by
using MongoDB Compass
◼ After successful import of CSV, EmployeeC appears in MongoDB
Compass.
|-UCCD2303/UCCD2203-| 51
View data or collection of documents by
using MongoDB Compass
◼ List View of data
|-UCCD2303/UCCD2203-| 52
View data or collection of documents by
using MongoDB Compass
◼ Table View of data
|-UCCD2303/UCCD2203-| 53
End of Practical 7
|-UCCD2303/UCCD2203-| 54