KEMBAR78
Introduction to mongodb | PPT
Introduction to MongoDB
Dr.A.Neela Madheswari,
Associate Professor,
Dept. of Computer Science and Engg,
Mahendra Engineering College,
Namakkal.
01/06/19 Introduction to MongoDB 1
2
Contents

MongoDB and Big Data

What is mongodb?

NoSQL

Where to use MongoDB?

Comparison of RDBMS & MongoDB

Database, Collection, Document

Installing MongoDB

Few commands and data types in MongoDB

Essential commands with screen shots
01/06/19 Introduction to MongoDB
3
MongoDB and Big Data
What makes data big?
Today data is being collected from different sources
and warehoused to perform analysis.
Different sources of data
Web data, E-commerce
Purchases at different stores
Bank/Credit card transactions
Social Network media
Every day 2.5 quintillion (10 to power of 18)bytes of
data generated.
90% of data we have today has been generated
from last two years01/06/19 Introduction to MongoDB
4
Who is generating Big data?
01/06/19 Introduction to MongoDB
5
Walmart handles more than 1 million customer
transactions every hour
Facebook handles 40 billion photos from its user base
Decoding the human genome originally took 10 years
to process; now it can be achieved in one week
01/06/19 Introduction to MongoDB
6
What is Hadoop?
Flexible architecture for large scale computation
and data processing on a network of
commodity hardware
Used for:
• Log processing
• Recommendation systems
• Analytics
• Video and image analysis
• Data retention
• etc
01/06/19 Introduction to MongoDB
7
MongoDB in Hadoop
01/06/19 Introduction to MongoDB
8
What is mongodb?

Document oriented database that provides
− High performance
− High availability
− Easy scalability

Open source

Leading NoSQL database

Written in C++

Creator: 10gen
01/06/19 Introduction to MongoDB
9
Non-relational data store
Does not store data in table
Stores data as a BSON document
BSON – Binary form of JSON
Stores data as a collection of key-value pair
Schema less
It means collection can have documents with
different shapes. Example:
• { “a”: 1, “b”: 2},
• {“a”: 1}
01/06/19 Introduction to MongoDB
10
NoSQL
Not only SQL
Non-relational, distributed, open-source and
horizontally scalable
Started in 2009 and growing rapidly
Characteristics:
Schema-free
Store huge amount of data
Currently more than 156 NoSQL databases
01/06/19 Introduction to MongoDB
11
NoSQL Databases categories
01/06/19 Introduction to MongoDB
1201/06/19 Introduction to MongoDB
13
Where to use mongodb?

Big data

Content management and Delivery

Mobile and Social Infrastructure

User data management

Data Hub
01/06/19 Introduction to MongoDB
14
Comparison of RDBMS & MongoDB
RDBMS Mongodb
Database Database
Table Collection
Tuple/Row Document
Column Field
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo
01/06/19 Introduction to MongoDB
15
Database

Database is a physical container for
collections.

Each database gets its own set of files on
the file system.

A single MongoDB server has multiple
databases.
01/06/19 Introduction to MongoDB
16
Collection

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.
01/06/19 Introduction to MongoDB
17
Document

A 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
– common fields in a collection's documents
may hold different types of data.
01/06/19 Introduction to MongoDB
1801/06/19 Introduction to MongoDB
19
Installing mongodb
01/06/19 Introduction to MongoDB
20
Steps for installing mongodb
1. Downloading the software and installing in
our system
2. Configuring system environment
3. Running mongodb
01/06/19 Introduction to MongoDB
21
1. Downloading the software and installing in
our system
01/06/19 Introduction to MongoDB
22
1. download the mongodb software i.e. msi file from the url
below:
https://www.mongodb.com/download-center#community
the downloaded software is: mongodb-win32-x86_64-
2008plus-ssl-3.2.10-signed.msi
2. Double click the .msi file to install.
3. Select Next button in Setup dialog box.
4. Accept the terms and conditions and select Next button.
5. Select setup type as Complete and click Next button.
6. Click Install button and then select Finish button.
7. Now a folder is created in the system with path:
C:Program FilesMongoDBServer3.2bin
01/06/19 Introduction to MongoDB
2301/06/19 Introduction to MongoDB
24
2. Configuring system environment
01/06/19 Introduction to MongoDB
25
1. MongoDB needs data directory to store all data. The
default path for data directory is: C:datadb
Hence create a folder with path as: C:datadb
2. Set the system environment path variable by right click
the My Computer or This PC icon on the Desktop ->
Select Advanced system settings -> Select Environment
variables -> In the system variables, select Path -> Click
Edit button -> Add the path at the end as: ;C:Program
FilesMongoDBServer3.2bin;
3. Now we can execute MongoDB commands at any path in
our system
01/06/19 Introduction to MongoDB
2601/06/19 Introduction to MongoDB
2701/06/19 Introduction to MongoDB
2801/06/19 Introduction to MongoDB
2901/06/19 Introduction to MongoDB
3001/06/19 Introduction to MongoDB
3101/06/19 Introduction to MongoDB
32
3. Running mongodb
01/06/19 Introduction to MongoDB
33
1. Go to cmd prompt and type as: mongod
Now the mongodb server has been started
on port 27017
2. Open another cmd prompt and type as:
mongo
Now we connected with mongodb server
with the database named test
01/06/19 Introduction to MongoDB
3401/06/19 Introduction to MongoDB
3501/06/19 Introduction to MongoDB
36
Commands - Help
To get commands, type as db.help() in client
01/06/19 Introduction to MongoDB
37
Mongodb Statistics
To get statistics about mongodb, type the
command db.stats() in client
01/06/19 Introduction to MongoDB
38
MongoDB Data types
S.No Data type Explanation
1 String String in MongoDB must be UTF-8
2 Integer Integer can be 32 bit or 64 bit depending upon our
server
3 Boolean To store values (true/false)
4 Double Store floating point values
5 Min/Max
keys
Used to compare a value against the lowest and
highest BSON elements
6 Arrays Used to store arrays or list or multiple values
7 Timestamp For recording when the document has been modified
8 Object Used for embedded documents
9 Null Used to store a Null value
10 Symbol Used to specify symbol type
11 Date Used to store current date or time in UNIX time format
12 Object ID Used to store document's ID
13 Binary data Used to store binary data
14 Code Used to store javascript code into the document
15 Regular
Expression
Used to store regular expression
01/06/19 Introduction to MongoDB
39
Essential Commands
1. Database
a) Create Database
b) Delete Database
2. Collection
a) Create collection
b) Drop collection
3. Document
a) Insert document
b) Query document
c) Update document
d) Remove document
01/06/19 Introduction to MongoDB
40
1.a) Create Database
Syntax:
use database_name
Example:
>use mydb
switched to db mydb
To check currently selected database, use the command db
>db
mydb
01/06/19 Introduction to MongoDB
4101/06/19 Introduction to MongoDB
42
To check the databases list, use the
command show dbs
>show dbs
01/06/19 Introduction to MongoDB
43
The databases mydb and test are not listed
To display the database, we need to insert
atleast one document into it.
In Mongodb, the default database is test.
If we dont create any database, the
collections will be stored in test database
01/06/19 Introduction to MongoDB
44
1.b) Delete Database
Syntax:
db.dropDatabase()
Example:
>use mydb
switched to db mydb
>db.dropDatabase()
>{ “dropped” : “mydb” , “ok” : 1 }
>
01/06/19 Introduction to MongoDB
45
2.a) Create Collection
Syntax:
db.createCollection(name, options)
Parameter Type Description
name String Name of the collection to be
created
options Document (optional) specify options
about memory size and
indexing
01/06/19 Introduction to MongoDB
4601/06/19 Introduction to MongoDB
47
2.b) Drop collection
Syntax:
db.collection_name.drop()
01/06/19 Introduction to MongoDB
48
3.a) Insert document
Syntax:
db.collection_name.insert(document)
01/06/19 Introduction to MongoDB
49
We can store multiple documents also at the
same time using insert() command like the
following.
01/06/19 Introduction to MongoDB
50
3.b) Query document
To query from MongoDB collection, we need
to use find() method.
Syntax:
db.collection_name.find()
This will display all the documents in a non-
structured way.
01/06/19 Introduction to MongoDB
5101/06/19 Introduction to MongoDB
52
To display the results in a formatted way,
pretty method is used.
Syntax:
db.collection_name.find().pretty()
01/06/19 Introduction to MongoDB
5301/06/19 Introduction to MongoDB
54
To return only one document, we have
findOne() method.
Syntax:
db.collection_name.findOne()
This will display the first document in the
given collection.
01/06/19 Introduction to MongoDB
5501/06/19 Introduction to MongoDB
56
3.c) Update document
The update() method is used to update the
values in the existing document.
Syntax:
db.collection_name.update(selection criteria,
updated data)
In the given example, the title is changed from 'MongoDB
Overview' to 'New MongoDB Tutorial' for title field.
By default, MongoDB will update only a single document. To
update multiple documents, we need to set a parameter
'multi' to true.01/06/19 Introduction to MongoDB
5701/06/19 Introduction to MongoDB
58
3.d) Remove document
remove() method is used to remove a
document from the collection.
Syntax:
db.collection_name.remove(deletion_criteria)
We can use deletion_criteria or justOne.
Both the values usage are optional only.
All the documents satisfying the given
criteria will be deleted.
01/06/19 Introduction to MongoDB
5901/06/19 Introduction to MongoDB
6001/06/19 Introduction to MongoDB
61
Across:
5. Used to find one document from given collection (7)
8. One of the NoSQL databases (7)
9. Database creation command (3)
10. Similar to table in MongoDB (10)
Down:
1. Mongodb server satrted with this port number (5)
2. Column family stores (5)
3. Set of key value pairs (8)
4. JavaScript Object Notation (4)
6. Not only SQL (5)
7. To get commands details (9)
11. Default database opened in MongoDB (4)01/06/19 Introduction to MongoDB
6201/06/19 Introduction to MongoDB
Online execution of queries
https://www.jdoodle.com/online-mongodb-terminal
01/06/19 Introduction to MongoDB 63
64
Thank You
01/06/19 Introduction to MongoDB

Introduction to mongodb

  • 1.
    Introduction to MongoDB Dr.A.NeelaMadheswari, Associate Professor, Dept. of Computer Science and Engg, Mahendra Engineering College, Namakkal. 01/06/19 Introduction to MongoDB 1
  • 2.
    2 Contents  MongoDB and BigData  What is mongodb?  NoSQL  Where to use MongoDB?  Comparison of RDBMS & MongoDB  Database, Collection, Document  Installing MongoDB  Few commands and data types in MongoDB  Essential commands with screen shots 01/06/19 Introduction to MongoDB
  • 3.
    3 MongoDB and BigData What makes data big? Today data is being collected from different sources and warehoused to perform analysis. Different sources of data Web data, E-commerce Purchases at different stores Bank/Credit card transactions Social Network media Every day 2.5 quintillion (10 to power of 18)bytes of data generated. 90% of data we have today has been generated from last two years01/06/19 Introduction to MongoDB
  • 4.
    4 Who is generatingBig data? 01/06/19 Introduction to MongoDB
  • 5.
    5 Walmart handles morethan 1 million customer transactions every hour Facebook handles 40 billion photos from its user base Decoding the human genome originally took 10 years to process; now it can be achieved in one week 01/06/19 Introduction to MongoDB
  • 6.
    6 What is Hadoop? Flexiblearchitecture for large scale computation and data processing on a network of commodity hardware Used for: • Log processing • Recommendation systems • Analytics • Video and image analysis • Data retention • etc 01/06/19 Introduction to MongoDB
  • 7.
    7 MongoDB in Hadoop 01/06/19Introduction to MongoDB
  • 8.
    8 What is mongodb?  Documentoriented database that provides − High performance − High availability − Easy scalability  Open source  Leading NoSQL database  Written in C++  Creator: 10gen 01/06/19 Introduction to MongoDB
  • 9.
    9 Non-relational data store Doesnot store data in table Stores data as a BSON document BSON – Binary form of JSON Stores data as a collection of key-value pair Schema less It means collection can have documents with different shapes. Example: • { “a”: 1, “b”: 2}, • {“a”: 1} 01/06/19 Introduction to MongoDB
  • 10.
    10 NoSQL Not only SQL Non-relational,distributed, open-source and horizontally scalable Started in 2009 and growing rapidly Characteristics: Schema-free Store huge amount of data Currently more than 156 NoSQL databases 01/06/19 Introduction to MongoDB
  • 11.
  • 12.
  • 13.
    13 Where to usemongodb?  Big data  Content management and Delivery  Mobile and Social Infrastructure  User data management  Data Hub 01/06/19 Introduction to MongoDB
  • 14.
    14 Comparison of RDBMS& MongoDB RDBMS Mongodb Database Database Table Collection Tuple/Row Document Column Field Database Server and Client Mysqld/Oracle mongod mysql/sqlplus mongo 01/06/19 Introduction to MongoDB
  • 15.
    15 Database  Database is aphysical container for collections.  Each database gets its own set of files on the file system.  A single MongoDB server has multiple databases. 01/06/19 Introduction to MongoDB
  • 16.
    16 Collection  Collection is agroup 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. 01/06/19 Introduction to MongoDB
  • 17.
    17 Document  A document isa 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 – common fields in a collection's documents may hold different types of data. 01/06/19 Introduction to MongoDB
  • 18.
  • 19.
  • 20.
    20 Steps for installingmongodb 1. Downloading the software and installing in our system 2. Configuring system environment 3. Running mongodb 01/06/19 Introduction to MongoDB
  • 21.
    21 1. Downloading thesoftware and installing in our system 01/06/19 Introduction to MongoDB
  • 22.
    22 1. download themongodb software i.e. msi file from the url below: https://www.mongodb.com/download-center#community the downloaded software is: mongodb-win32-x86_64- 2008plus-ssl-3.2.10-signed.msi 2. Double click the .msi file to install. 3. Select Next button in Setup dialog box. 4. Accept the terms and conditions and select Next button. 5. Select setup type as Complete and click Next button. 6. Click Install button and then select Finish button. 7. Now a folder is created in the system with path: C:Program FilesMongoDBServer3.2bin 01/06/19 Introduction to MongoDB
  • 23.
  • 24.
    24 2. Configuring systemenvironment 01/06/19 Introduction to MongoDB
  • 25.
    25 1. MongoDB needsdata directory to store all data. The default path for data directory is: C:datadb Hence create a folder with path as: C:datadb 2. Set the system environment path variable by right click the My Computer or This PC icon on the Desktop -> Select Advanced system settings -> Select Environment variables -> In the system variables, select Path -> Click Edit button -> Add the path at the end as: ;C:Program FilesMongoDBServer3.2bin; 3. Now we can execute MongoDB commands at any path in our system 01/06/19 Introduction to MongoDB
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
    32 3. Running mongodb 01/06/19Introduction to MongoDB
  • 33.
    33 1. Go tocmd prompt and type as: mongod Now the mongodb server has been started on port 27017 2. Open another cmd prompt and type as: mongo Now we connected with mongodb server with the database named test 01/06/19 Introduction to MongoDB
  • 34.
  • 35.
  • 36.
    36 Commands - Help Toget commands, type as db.help() in client 01/06/19 Introduction to MongoDB
  • 37.
    37 Mongodb Statistics To getstatistics about mongodb, type the command db.stats() in client 01/06/19 Introduction to MongoDB
  • 38.
    38 MongoDB Data types S.NoData type Explanation 1 String String in MongoDB must be UTF-8 2 Integer Integer can be 32 bit or 64 bit depending upon our server 3 Boolean To store values (true/false) 4 Double Store floating point values 5 Min/Max keys Used to compare a value against the lowest and highest BSON elements 6 Arrays Used to store arrays or list or multiple values 7 Timestamp For recording when the document has been modified 8 Object Used for embedded documents 9 Null Used to store a Null value 10 Symbol Used to specify symbol type 11 Date Used to store current date or time in UNIX time format 12 Object ID Used to store document's ID 13 Binary data Used to store binary data 14 Code Used to store javascript code into the document 15 Regular Expression Used to store regular expression 01/06/19 Introduction to MongoDB
  • 39.
    39 Essential Commands 1. Database a)Create Database b) Delete Database 2. Collection a) Create collection b) Drop collection 3. Document a) Insert document b) Query document c) Update document d) Remove document 01/06/19 Introduction to MongoDB
  • 40.
    40 1.a) Create Database Syntax: usedatabase_name Example: >use mydb switched to db mydb To check currently selected database, use the command db >db mydb 01/06/19 Introduction to MongoDB
  • 41.
  • 42.
    42 To check thedatabases list, use the command show dbs >show dbs 01/06/19 Introduction to MongoDB
  • 43.
    43 The databases mydband test are not listed To display the database, we need to insert atleast one document into it. In Mongodb, the default database is test. If we dont create any database, the collections will be stored in test database 01/06/19 Introduction to MongoDB
  • 44.
    44 1.b) Delete Database Syntax: db.dropDatabase() Example: >usemydb switched to db mydb >db.dropDatabase() >{ “dropped” : “mydb” , “ok” : 1 } > 01/06/19 Introduction to MongoDB
  • 45.
    45 2.a) Create Collection Syntax: db.createCollection(name,options) Parameter Type Description name String Name of the collection to be created options Document (optional) specify options about memory size and indexing 01/06/19 Introduction to MongoDB
  • 46.
  • 47.
  • 48.
  • 49.
    49 We can storemultiple documents also at the same time using insert() command like the following. 01/06/19 Introduction to MongoDB
  • 50.
    50 3.b) Query document Toquery from MongoDB collection, we need to use find() method. Syntax: db.collection_name.find() This will display all the documents in a non- structured way. 01/06/19 Introduction to MongoDB
  • 51.
  • 52.
    52 To display theresults in a formatted way, pretty method is used. Syntax: db.collection_name.find().pretty() 01/06/19 Introduction to MongoDB
  • 53.
  • 54.
    54 To return onlyone document, we have findOne() method. Syntax: db.collection_name.findOne() This will display the first document in the given collection. 01/06/19 Introduction to MongoDB
  • 55.
  • 56.
    56 3.c) Update document Theupdate() method is used to update the values in the existing document. Syntax: db.collection_name.update(selection criteria, updated data) In the given example, the title is changed from 'MongoDB Overview' to 'New MongoDB Tutorial' for title field. By default, MongoDB will update only a single document. To update multiple documents, we need to set a parameter 'multi' to true.01/06/19 Introduction to MongoDB
  • 57.
  • 58.
    58 3.d) Remove document remove()method is used to remove a document from the collection. Syntax: db.collection_name.remove(deletion_criteria) We can use deletion_criteria or justOne. Both the values usage are optional only. All the documents satisfying the given criteria will be deleted. 01/06/19 Introduction to MongoDB
  • 59.
  • 60.
  • 61.
    61 Across: 5. Used tofind one document from given collection (7) 8. One of the NoSQL databases (7) 9. Database creation command (3) 10. Similar to table in MongoDB (10) Down: 1. Mongodb server satrted with this port number (5) 2. Column family stores (5) 3. Set of key value pairs (8) 4. JavaScript Object Notation (4) 6. Not only SQL (5) 7. To get commands details (9) 11. Default database opened in MongoDB (4)01/06/19 Introduction to MongoDB
  • 62.
  • 63.
    Online execution ofqueries https://www.jdoodle.com/online-mongodb-terminal 01/06/19 Introduction to MongoDB 63
  • 64.