0% found this document useful (0 votes)
2 views

UCCD2303_Practical07_slides_MongoDB

This document provides a practical introduction to NoSQL databases, specifically focusing on MongoDB. It covers installation, usage of the Mongo shell, and methods for importing CSV data using both command line and MongoDB Compass. Additionally, it highlights the advantages of MongoDB and its application in various scenarios.

Uploaded by

leezy886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UCCD2303_Practical07_slides_MongoDB

This document provides a practical introduction to NoSQL databases, specifically focusing on MongoDB. It covers installation, usage of the Mongo shell, and methods for importing CSV data using both command line and MongoDB Compass. Additionally, it highlights the advantages of MongoDB and its application in various scenarios.

Uploaded by

leezy886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

UCCD2303 DATABASE TECHNOLOGY /

UCCD2203 DATABASE SYSTEMS

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

❖ Introduction to NoSQL Database and MongoDB


❖ Installing NoSQL Database – MongoDB
❖ Introduction to Mongo Shell
❖ Import CSV data with mongoimport.exe
❖ Import CSV data manually with MongoDB
Compass, a MongoDB Graphical User Interface
(GUI)
❖ View data or collection of documents by using
MongoDB Compass

|-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

◼ MongoDB for Windows 64-bit - runs only on Windows Server 2008


R2, Windows 7 64-bit, and newer versions of Windows. This build
takes advantage of recent enhancements to the Windows Platform
and cannot operate on older versions of Windows.

◼ MongoDB for Windows 32-bit - runs on any 32-bit version of


Windows newer than Windows Vista. 32-bit versions of MongoDB
are only intended for older systems and for use in testing and
development systems.((https://www.mongodb.org/dl/win32/i386)

|-UCCD2303/UCCD2203-| 19
Installing MongoDB

◼ Files for installation:


❑ mongodb-windows-x86_64-4.4.6-signed.exe
❑ mongodb-database-tools-windows-x86_64-100.3.1.zip

|-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

◼ Mongo is an interactive JavaScript shell interface to


MongoDB, which provides a powerful interface for
system administrators as well as a way for developers to
test queries and operations directly with the database.
◼ Mongo also provides a fully functional JavaScript
environment for use with a MongoDB.
◼ The mongo shell is included as part of the MongoDB
server installation. If you have already installed the
server, the mongo shell is installed to the same location
as the server binary.

|-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

◼ Open another Command Prompt:


❑ Type mongo to open the client to run the MongoDB commands

|-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.

◼ Copy all the files from mongodb-database-


tools-windows-x86_64-100.3.1\bin into
C:\Program Files\MongoDB\Server\4.4\bin

|-UCCD2303/UCCD2203-| 33
Import CSV data with mongoimport.exe

◼ Copy and paste “Employee.csv” to C:\db


*Note: create a folder called ‘db’ to store all your databases
❑ Employee.csv consists of Employee - their first and last name, age,

email, education, etc.


❑ In addition, it is a flat data set which is perfect for MongoDB. Primary

Key of id has been removed as MongoDB’s default functionality of


giving each document (row) a unique id of its own.
◼ To view the data files created by MongoDB - C:\Program
Files\MongoDB\Server\4.4\data

|-UCCD2303/UCCD2203-| 34
Import CSV data with mongoimport.exe

◼ Open Command Prompt by typing ‘cmd’

|-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

◼ Make sure mongoimport.exe is available in C:\Program Files\MongoDB\Server\4.4\bin


◼ Type:
mongoimport --db EmployeeD --collection EmployeeC --type csv --headerline --file
d:\Employee.csv
◼ Syntax:
mongoimport –db database_name –collection collection_name –type csv –file
file_location

|-UCCD2303/UCCD2203-| 37
Import CSV data with mongoimport.exe

◼ mongoimport is the command to import data


◼ --type csv indicates that this is a CSV file
◼ --headerline indicates that the file row of your CSV is the header
◼ -- db EmployeeD indicates that the database we are writing to is
EmployeeD
◼ --collection indicates that the collection we are writing to is EmployeeC
◼ --host "<STRING>" indicates the route to our database. Replace it with
the string you copied
◼ --username analytics --password analytics-pw indicates your user
account. Replace with your username and password
◼ --file Employee.csv indicates the file name of your data.

|-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

You might also like