SlideShare a Scribd company logo
1
Getting Started with
MongoDB
TCF IT Professional Conference
March 14, 2014
Michael P. Redlich
@mpredli
about.me/mpredli/
Sunday, March 16, 14
Who’s Mike?
• BS in CS from
• “Petrochemical Research Organization”
• Ai-Logix, Inc. (now AudioCodes)
• Amateur Computer Group of New Jersey
• Publications
• Presentations
2
Sunday, March 16, 14
Objectives
• What is MongoDB?
• What is NoSQL?
• Getting Started with MongoDB
• Basic CRUD Operations
• Live Demos (yea!)
• MongoDB Resources
3
Sunday, March 16, 14
What is MongoDB? (1)
• “...an open-source document database that
provides high performance, high availability, and
automatic scaling.”
MongoDB Web Site, http://www.mongodb.org/
• It’s name derived from “humongous”
• Written in C++
4
Sunday, March 16, 14
What is MongoDB? (2)
• “...an open-source database used by
companies of all sizes, across all industries and
for a wide variety of applications. It is an agile
database that allows schemas to change
quickly as applications evolve, while still
providing functionality developers expect from
traditional databases...”
MongoDB Products Web Site, http://www.mongodb.com/products/mongodb/
5
Sunday, March 16, 14
What is NoSQL?
• Developed to address shortcomings of a
traditional SQL relational database, namely:
• big data
• frequency of access to big data
• performance and scalability
6
Sunday, March 16, 14
How is MongoDB
Used?
7
Sunday, March 16, 14
Who is Using
MongoDB?
8
Sunday, March 16, 14
Features of MongoDB
• Document-Oriented
Storage
• Full Index Support
• Replication and High
Availability
• Auto-Sharding
• Querying
• Fast In-Place Updates
• Map/Reduce
• GridFS
• Professional Support by
MongoDB
9
Sunday, March 16, 14
Nomenclature (1)
10
RDBMS MongoDB
Database Database
Table Collection
Row Document
Index Index
Join Embedding & Linking
Foreign Key Reference
Sunday, March 16, 14
Nomenclature (2)
11
Sunday, March 16, 14
What is a Document?
• Basic unit of data
• analogous to a row in a RDBMS
• An ordered set of fields (keys) with
associated values stored in BSON format
• similar to JSON
12
Sunday, March 16, 14
What is BSON?
• “...a binary-encoded serialization of JSON-like
documents.”
BSON Web Site, http://www.bsonspec.org/
• Binary JSON
• Designed to be lightweight, traversable, and
efficient
13
Sunday, March 16, 14
What is a Collection?
• A group of documents
• analogous to a table in a RDBMS
• Schema-less
14
Sunday, March 16, 14
Advantages of
Documents
• Documents correspond to native data
types in many programming languages
• Embedded documents and arrays reduce
the need for expensive joins
• Dynamic schema support fluent
polymorphism
15
Sunday, March 16, 14
Document Structure
{
lastName : “Redlich”,
firstName : “Michael”,
email : “mike@redlich.net”
role : {
officer : “President”,
sig : “Java Users Group”
}
}
16
field value
embedded document
Sunday, March 16, 14
Field Names
• Strings
• Cannot contain:
• null
• dots (.)
• dollar sign ($)
• No duplicate field names
17
Sunday, March 16, 14
Conventions Used in
This Presentation
• Command Prompt ($)
• MySQL prompt (mysql>)
• MongoDB prompt (>)
• Keywords (db, find(), etc.)
• Variables (variable)
18
Sunday, March 16, 14
Example Database
• ACGNJ Board of Directors:
• lastName
• firstName
• roles (embedded documents)
• tenure
19
Sunday, March 16, 14
Getting Started
• Download MongoDB
• Create a default data directory
•/data/db
•C:datadb
• Create your first MongoDB database
20
Sunday, March 16, 14
Starting MongoDB
• Start an instance of the MongoDB server:
$ mongod
• Start an instance of the MongoDB client (a
JavaScript-based shell):
$ mongo
21
Sunday, March 16, 14
Mongo Shell (1)
• Show the list of shell commands:
> help
• Show the list of databases:
> show dbs
• Show the current database:
> db
22
Sunday, March 16, 14
Mongo Shell (2)
• Specify the database to use or create:
> use database
• Show the collections within the current
database:
> show collections
• Show the users within the database:
> show users
23
Sunday, March 16, 14
Mongo Shell (3)
• Show the recent system.profile
entries:
> show profile
• Tab completion
• Command history
24
Sunday, March 16, 14
Primary Key
• Denoted by a special field, _id
• It can be generated:
• Implicitly:
• {_id : ObjectID(value)}
• Explicitly:
• {_id : 2 }, { _id : “MPR”}
25
Sunday, March 16, 14
ObjectIDs
• Default type for _id
• A 12-byte hexadecimal BSON type:
26
Sunday, March 16, 14
Live Demo!
27
Sunday, March 16, 14
Create
28
Sunday, March 16, 14
Create a Database
• Create a database in MySQL:
mysql> CREATE DATABASE database;
• Create a database in MongoDB:
> use database
29
Sunday, March 16, 14
Create a Collection
• Create a new table in MySQL:
mysql> CREATE TABLE table(column
datatype,...);
• Create a new collection in MongoDB:
>
db.collection.insert({field:value,.
..})
30
Sunday, March 16, 14
Insert Data
• Insert a row in MySQL:
> INSERT INTO table(column,...)
VALUES(value,...);
• Insert a document in MongoDB:
>
db.collection.insert({field:value,.
..})
31
Sunday, March 16, 14
Insert Data with Loops
• Insert multiple documents with an array:
> for(int i = 0;i < j;++i)
db.collection.insert({field:array[i
]});
• Insert multiple documents with variable:
> for(int i = 0;i < j;++i)
db.collection.insert({field:i})
32
Sunday, March 16, 14
Live Demo!
33
Sunday, March 16, 14
Read
34
Sunday, March 16, 14
Query (1)
• Retrieve all rows in MySQL:
mysql> SELECT * FROM table;
• Retrieve all documents in MongoDB:
> db.collection.find()
35
Sunday, March 16, 14
Query (2)
• Retrieve specified columns in MySQL:
mysql> SELECT column1,column2 FROM
table;
• Retrieve specified fields in MongoDB:
> db.collection.find({},
{field1:true,field2:true})
36
Sunday, March 16, 14
Query (3)
• Retrieve specific rows in MySQL:
mysql> SELECT * FROM table WHERE
column = value;
• Retrieve specific documents in MongoDB:
> db.collection.find({field:value})
37
Sunday, March 16, 14
Query (4)
• Retrieve specific rows in MySQL:
mysql> SELECT * FROM table WHERE
column = value ORDER BY value ASC;
• Retrieve specific documents in MongoDB:
>
db.collection.find({field:value}).s
ort({field:1})
38
Sunday, March 16, 14
Query (5)
• Query for multiple documents (returns a
cursor):
> db.collection.find()
• Query for one document (returns a single
document):
> db.collection.findOne()
39
Sunday, March 16, 14
Query Selectors
• Scalar:
• $ne, $mod, $exists, $type, $lt, $lte,
$gt, $gte
• Vector:
• $in, $nin, $all, $size
40
Sunday, March 16, 14
Query (6)
• Retrieve specific rows in MySQL:
mysql> SELECT * FROM table WHERE
column != value;
• Retrieve specific documents in MongoDB:
> db.collection.find({field:
{$ne:value}})
41
Sunday, March 16, 14
Query (7)
• Retrieve specific rows in MySQL:
mysql> SELECT * FROM table WHERE
column1 = value OR column2 = value;
• Retrieve specific documents in MongoDB:
> db.collection.find({$or:
[{field:value},{field:value}])
42
Sunday, March 16, 14
Query (8)
> db.members.aggregate({$project:
{officer:"$roles.officer"}})
> db.members.find({tenure:
{$gt:ISODate("2014-12-31")}})
> db.members.find({"roles.officer":
{$exists:true}}).sort({"roles.offic
er":1})
43
Sunday, March 16, 14
Query (9)
>
db.members.find({"roles.director":
{$all:["Director"]}})
>
db.members.find({"roles.committee":
{$in:["Historian","Newsletter"]}})
> db.members.find({roles:{$size:
3}})
44
Sunday, March 16, 14
Live Demo!
45
Sunday, March 16, 14
Update
46
Sunday, March 16, 14
Update (1)
• Update a row in MySQL:
mysql> UPDATE table SET column =
value WHERE id = id;
• Update a document in a MongoDB:
> db.collection.update({_id:value},
{$set:{field:value}},{multi:true})
47
Sunday, March 16, 14
Update (2)
• Update a row in MySQL:
mysql> UPDATE table SET column1 =
value WHERE column2 > value;
• Update a document in MongoDB:
> db.collection.update({field1:
{$gt:value}},{$set:{field2:value}},
{multi:true})
48
Sunday, March 16, 14
Update (3)
• Update a document using findOne():
> redlich =
db.members.findOne({lastName:
"Redlich"})
> redlich.roles = [{sig:"Java Users
Group"}]
> db.members.update({lastName:
"Redlich"},redlich)
49
Sunday, March 16, 14
Atomic Update
Operators
• Scalar:
• $inc, $set, $unset
• Vector:
• $push, $pop, $pull, $pushAll,
$pullAll, $addToSet
50
Sunday, March 16, 14
Update (4)
> db.members.update({lastName:
"Redlich"},{$set:
{"ISODate("2016-12-31")}})
> db.members.update({"roles.sig"},
{$set:{"roles.sig":"JUG"}})
51
Sunday, March 16, 14
Delete
52
Sunday, March 16, 14
Delete (1)
• Delete all rows in MySQL:
mysql> DELETE FROM table;
• Delete all documents in MongoDB:
> db.collection.remove()
53
Sunday, March 16, 14
Delete (2)
• Delete specific rows in MySQL:
mysql> DELETE FROM table WHERE
column = value;
• Delete specific documents in MongoDB:
>
db.collection.remove({field:value})
54
Sunday, March 16, 14
Delete (2)
• Delete a MySQL database
mysql> DROP DATABASE database;
• Delete a MongoDB database
> use database
> db.dropDatabase()
55
Sunday, March 16, 14
Backup/Restore
56
Sunday, March 16, 14
Export (1)
• Export a collection to a JSON file
• Ensure mongod is running
$ mongoexport --db database --
collection collection --out path/
filename.json
57
Sunday, March 16, 14
Export (2)
• Export a collection to a CSV file
• Ensure mongod is running
• A list of fields is required
$ mongoexport --db database --
collection collection --fields
field1,field2,... --csv --out path/
filename.json
58
Sunday, March 16, 14
Import
• Import a collection from a JSON, CSV, or
TSV file
• Ensure mongod is running
$ mongoimport --db database --
collection collection < path/
filename.json
59
Sunday, March 16, 14
Dump
• Dump a specified MySQL database:
$ mysqldump -u root --opt database
> path.filename.sql
• Dump all MongoDB databases:
• Ensure mongod is not running
$ mongodump --dbpath /data/db --out
path
60
Sunday, March 16, 14
Live Demo!
61
Sunday, March 16, 14
Package Components
(1)
• Core Processes
• mongod - core DB process
• mongos - controller & query router (sharding)
• mongo - interactive JavaScript-based shell
62
Sunday, March 16, 14
Package Components
(2)
• Binary Import and Export
• mongodump - creates BSON dump files
• mongorestore - restores BSON dump files
• bsondump - converts BSON to JSON
• mongooplog - streams oplog entries
63
Sunday, March 16, 14
Package Components
(3)
• Data Import and Export
• mongoimport - imports JSON, CSV, or TSV
data formats
• mongoexport - exports to JSON, CSV, or TSV
data formats
64
Sunday, March 16, 14
Package Components
(4)
• Diagnostic Tools
• mongostat - captures database operations by
type (insert, query, etc.)
• mongotop - tracks read/write activity
• mongosniff - provides tracing/sniffing view
into database activity
• mongoperf - performance testing tool
65
Sunday, March 16, 14
Package Components
(5)
• GridFS
• mongofiles - provides a command-line
interaction to a GridFS storage system
66
Sunday, March 16, 14
MongoDB Resources
(1)
67
Sunday, March 16, 14
68
MongoDB Resources
(2)
•mongodb.org
•docs.mongodb.org
•mongodb.org/books
•mongodb.com/products/mongodb
•mongodb.com/reference
•bsonspec.org
•education.mongodb.com
Sunday, March 16, 14
Upcoming Events (1)
• Trenton Computer Festival
• March 14-15, 2014
• tcf-nj.org
• Emerging Technologies for the Enterprise
• April 22-23, 2014
• phillyemergingtech.com
69
Sunday, March 16, 14
70
Upcoming Events (2)
Sunday, March 16, 14
71
Thanks!
mike@redlich.net
@mpredli
javasig.org
Sunday, March 16, 14

More Related Content

KEY
MongoDB at GUL
PDF
An Overview of Data Management Paradigms: Relational, Document, and Graph
PPTX
PDF
Mongo db basics
PDF
Using MongoDB and Python
PPT
Introduction to MongoDB
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PDF
Mongo db basics
MongoDB at GUL
An Overview of Data Management Paradigms: Relational, Document, and Graph
Mongo db basics
Using MongoDB and Python
Introduction to MongoDB
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo db basics

What's hot (20)

PPT
2011 Mongo FR - MongoDB introduction
PPTX
Mongo db
PPTX
Chen li asterix db: 大数据处理开源平台
PPTX
Mongo DB 102
PDF
Mongo db
PPT
Introduction to MongoDB
PPT
2011 mongo FR - scaling with mongodb
PPTX
Mongo DB Presentation
PDF
Intro To MongoDB
PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
PDF
Full metal mongo
PPTX
Get expertise with mongo db
PDF
Python Files
KEY
MongoDB and hadoop
PDF
JSONpedia - Facilitating consumption of MediaWiki content
PDF
Introduction to mongoDB
PPTX
Back to Basics, webinar 2: La tua prima applicazione MongoDB
PPT
5 Pitfalls to Avoid with MongoDB
PPTX
MongoDB basics & Introduction
PPTX
Connecting NodeJS & MongoDB
2011 Mongo FR - MongoDB introduction
Mongo db
Chen li asterix db: 大数据处理开源平台
Mongo DB 102
Mongo db
Introduction to MongoDB
2011 mongo FR - scaling with mongodb
Mongo DB Presentation
Intro To MongoDB
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Full metal mongo
Get expertise with mongo db
Python Files
MongoDB and hadoop
JSONpedia - Facilitating consumption of MediaWiki content
Introduction to mongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDB
5 Pitfalls to Avoid with MongoDB
MongoDB basics & Introduction
Connecting NodeJS & MongoDB
Ad

Similar to Getting Started with MongoDB (TCF ITPC 2014) (20)

PDF
Getting Started with MongoDB
PDF
MongoDB: a gentle, friendly overview
KEY
Mongodb intro
PDF
Starting with MongoDB
PDF
Introduction to Mongo DB-open-­‐source, high-­‐performance, document-­‐orient...
PDF
Mongo db php_shaken_not_stirred_joomlafrappe
PDF
Getting Started with C++ (TCF 2014)
PDF
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
KEY
MongoDB Strange Loop 2009
PPTX
Indexing Strategies to Help You Scale
PPTX
MongoDB using Grails plugin by puneet behl
KEY
ODP
PDF
How to use MongoDB with CakePHP
KEY
MongoDB at CodeMash 2.0.1.0
PPTX
MongoDB_ppt.pptx
KEY
MongoDB NYC Python
PDF
MongoDB for Coder Training (Coding Serbia 2013)
PPTX
RethinkDB - the open-source database for the realtime web
PPTX
Basics of MongoDB
Getting Started with MongoDB
MongoDB: a gentle, friendly overview
Mongodb intro
Starting with MongoDB
Introduction to Mongo DB-open-­‐source, high-­‐performance, document-­‐orient...
Mongo db php_shaken_not_stirred_joomlafrappe
Getting Started with C++ (TCF 2014)
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
MongoDB Strange Loop 2009
Indexing Strategies to Help You Scale
MongoDB using Grails plugin by puneet behl
How to use MongoDB with CakePHP
MongoDB at CodeMash 2.0.1.0
MongoDB_ppt.pptx
MongoDB NYC Python
MongoDB for Coder Training (Coding Serbia 2013)
RethinkDB - the open-source database for the realtime web
Basics of MongoDB
Ad

More from Michael Redlich (20)

PDF
Getting Started with GitHub
PDF
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
PDF
Building Microservices with Helidon: Oracle's New Java Microservices Framework
PDF
Introduction to Object Oriented Programming & Design Principles
PDF
Getting Started with C++
PDF
C++ Advanced Features
PDF
Java Advanced Features
PDF
Introduction to Object Oriented Programming &amp; Design Principles
PDF
Getting Started with Java
PDF
Getting started with C++
PDF
C++ Advanced Features
PDF
Building Realtime Access to Data Apps with jOOQ
PDF
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
PDF
Building Realtime Web Apps with Angular and Meteor
PDF
Java Advanced Features (TCF 2014)
PDF
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
PDF
Getting Started with Meteor (TCF ITPC 2014)
PDF
Getting Started with Java (TCF 2014)
PDF
C++ Advanced Features (TCF 2014)
PDF
Getting Started with Meteor
Getting Started with GitHub
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Building Microservices with Helidon: Oracle's New Java Microservices Framework
Introduction to Object Oriented Programming & Design Principles
Getting Started with C++
C++ Advanced Features
Java Advanced Features
Introduction to Object Oriented Programming &amp; Design Principles
Getting Started with Java
Getting started with C++
C++ Advanced Features
Building Realtime Access to Data Apps with jOOQ
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Web Apps with Angular and Meteor
Java Advanced Features (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Java (TCF 2014)
C++ Advanced Features (TCF 2014)
Getting Started with Meteor

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.

Getting Started with MongoDB (TCF ITPC 2014)

  • 1. 1 Getting Started with MongoDB TCF IT Professional Conference March 14, 2014 Michael P. Redlich @mpredli about.me/mpredli/ Sunday, March 16, 14
  • 2. Who’s Mike? • BS in CS from • “Petrochemical Research Organization” • Ai-Logix, Inc. (now AudioCodes) • Amateur Computer Group of New Jersey • Publications • Presentations 2 Sunday, March 16, 14
  • 3. Objectives • What is MongoDB? • What is NoSQL? • Getting Started with MongoDB • Basic CRUD Operations • Live Demos (yea!) • MongoDB Resources 3 Sunday, March 16, 14
  • 4. What is MongoDB? (1) • “...an open-source document database that provides high performance, high availability, and automatic scaling.” MongoDB Web Site, http://www.mongodb.org/ • It’s name derived from “humongous” • Written in C++ 4 Sunday, March 16, 14
  • 5. What is MongoDB? (2) • “...an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to change quickly as applications evolve, while still providing functionality developers expect from traditional databases...” MongoDB Products Web Site, http://www.mongodb.com/products/mongodb/ 5 Sunday, March 16, 14
  • 6. What is NoSQL? • Developed to address shortcomings of a traditional SQL relational database, namely: • big data • frequency of access to big data • performance and scalability 6 Sunday, March 16, 14
  • 9. Features of MongoDB • Document-Oriented Storage • Full Index Support • Replication and High Availability • Auto-Sharding • Querying • Fast In-Place Updates • Map/Reduce • GridFS • Professional Support by MongoDB 9 Sunday, March 16, 14
  • 10. Nomenclature (1) 10 RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign Key Reference Sunday, March 16, 14
  • 12. What is a Document? • Basic unit of data • analogous to a row in a RDBMS • An ordered set of fields (keys) with associated values stored in BSON format • similar to JSON 12 Sunday, March 16, 14
  • 13. What is BSON? • “...a binary-encoded serialization of JSON-like documents.” BSON Web Site, http://www.bsonspec.org/ • Binary JSON • Designed to be lightweight, traversable, and efficient 13 Sunday, March 16, 14
  • 14. What is a Collection? • A group of documents • analogous to a table in a RDBMS • Schema-less 14 Sunday, March 16, 14
  • 15. Advantages of Documents • Documents correspond to native data types in many programming languages • Embedded documents and arrays reduce the need for expensive joins • Dynamic schema support fluent polymorphism 15 Sunday, March 16, 14
  • 16. Document Structure { lastName : “Redlich”, firstName : “Michael”, email : “mike@redlich.net” role : { officer : “President”, sig : “Java Users Group” } } 16 field value embedded document Sunday, March 16, 14
  • 17. Field Names • Strings • Cannot contain: • null • dots (.) • dollar sign ($) • No duplicate field names 17 Sunday, March 16, 14
  • 18. Conventions Used in This Presentation • Command Prompt ($) • MySQL prompt (mysql>) • MongoDB prompt (>) • Keywords (db, find(), etc.) • Variables (variable) 18 Sunday, March 16, 14
  • 19. Example Database • ACGNJ Board of Directors: • lastName • firstName • roles (embedded documents) • tenure 19 Sunday, March 16, 14
  • 20. Getting Started • Download MongoDB • Create a default data directory •/data/db •C:datadb • Create your first MongoDB database 20 Sunday, March 16, 14
  • 21. Starting MongoDB • Start an instance of the MongoDB server: $ mongod • Start an instance of the MongoDB client (a JavaScript-based shell): $ mongo 21 Sunday, March 16, 14
  • 22. Mongo Shell (1) • Show the list of shell commands: > help • Show the list of databases: > show dbs • Show the current database: > db 22 Sunday, March 16, 14
  • 23. Mongo Shell (2) • Specify the database to use or create: > use database • Show the collections within the current database: > show collections • Show the users within the database: > show users 23 Sunday, March 16, 14
  • 24. Mongo Shell (3) • Show the recent system.profile entries: > show profile • Tab completion • Command history 24 Sunday, March 16, 14
  • 25. Primary Key • Denoted by a special field, _id • It can be generated: • Implicitly: • {_id : ObjectID(value)} • Explicitly: • {_id : 2 }, { _id : “MPR”} 25 Sunday, March 16, 14
  • 26. ObjectIDs • Default type for _id • A 12-byte hexadecimal BSON type: 26 Sunday, March 16, 14
  • 29. Create a Database • Create a database in MySQL: mysql> CREATE DATABASE database; • Create a database in MongoDB: > use database 29 Sunday, March 16, 14
  • 30. Create a Collection • Create a new table in MySQL: mysql> CREATE TABLE table(column datatype,...); • Create a new collection in MongoDB: > db.collection.insert({field:value,. ..}) 30 Sunday, March 16, 14
  • 31. Insert Data • Insert a row in MySQL: > INSERT INTO table(column,...) VALUES(value,...); • Insert a document in MongoDB: > db.collection.insert({field:value,. ..}) 31 Sunday, March 16, 14
  • 32. Insert Data with Loops • Insert multiple documents with an array: > for(int i = 0;i < j;++i) db.collection.insert({field:array[i ]}); • Insert multiple documents with variable: > for(int i = 0;i < j;++i) db.collection.insert({field:i}) 32 Sunday, March 16, 14
  • 35. Query (1) • Retrieve all rows in MySQL: mysql> SELECT * FROM table; • Retrieve all documents in MongoDB: > db.collection.find() 35 Sunday, March 16, 14
  • 36. Query (2) • Retrieve specified columns in MySQL: mysql> SELECT column1,column2 FROM table; • Retrieve specified fields in MongoDB: > db.collection.find({}, {field1:true,field2:true}) 36 Sunday, March 16, 14
  • 37. Query (3) • Retrieve specific rows in MySQL: mysql> SELECT * FROM table WHERE column = value; • Retrieve specific documents in MongoDB: > db.collection.find({field:value}) 37 Sunday, March 16, 14
  • 38. Query (4) • Retrieve specific rows in MySQL: mysql> SELECT * FROM table WHERE column = value ORDER BY value ASC; • Retrieve specific documents in MongoDB: > db.collection.find({field:value}).s ort({field:1}) 38 Sunday, March 16, 14
  • 39. Query (5) • Query for multiple documents (returns a cursor): > db.collection.find() • Query for one document (returns a single document): > db.collection.findOne() 39 Sunday, March 16, 14
  • 40. Query Selectors • Scalar: • $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte • Vector: • $in, $nin, $all, $size 40 Sunday, March 16, 14
  • 41. Query (6) • Retrieve specific rows in MySQL: mysql> SELECT * FROM table WHERE column != value; • Retrieve specific documents in MongoDB: > db.collection.find({field: {$ne:value}}) 41 Sunday, March 16, 14
  • 42. Query (7) • Retrieve specific rows in MySQL: mysql> SELECT * FROM table WHERE column1 = value OR column2 = value; • Retrieve specific documents in MongoDB: > db.collection.find({$or: [{field:value},{field:value}]) 42 Sunday, March 16, 14
  • 43. Query (8) > db.members.aggregate({$project: {officer:"$roles.officer"}}) > db.members.find({tenure: {$gt:ISODate("2014-12-31")}}) > db.members.find({"roles.officer": {$exists:true}}).sort({"roles.offic er":1}) 43 Sunday, March 16, 14
  • 47. Update (1) • Update a row in MySQL: mysql> UPDATE table SET column = value WHERE id = id; • Update a document in a MongoDB: > db.collection.update({_id:value}, {$set:{field:value}},{multi:true}) 47 Sunday, March 16, 14
  • 48. Update (2) • Update a row in MySQL: mysql> UPDATE table SET column1 = value WHERE column2 > value; • Update a document in MongoDB: > db.collection.update({field1: {$gt:value}},{$set:{field2:value}}, {multi:true}) 48 Sunday, March 16, 14
  • 49. Update (3) • Update a document using findOne(): > redlich = db.members.findOne({lastName: "Redlich"}) > redlich.roles = [{sig:"Java Users Group"}] > db.members.update({lastName: "Redlich"},redlich) 49 Sunday, March 16, 14
  • 50. Atomic Update Operators • Scalar: • $inc, $set, $unset • Vector: • $push, $pop, $pull, $pushAll, $pullAll, $addToSet 50 Sunday, March 16, 14
  • 51. Update (4) > db.members.update({lastName: "Redlich"},{$set: {"ISODate("2016-12-31")}}) > db.members.update({"roles.sig"}, {$set:{"roles.sig":"JUG"}}) 51 Sunday, March 16, 14
  • 53. Delete (1) • Delete all rows in MySQL: mysql> DELETE FROM table; • Delete all documents in MongoDB: > db.collection.remove() 53 Sunday, March 16, 14
  • 54. Delete (2) • Delete specific rows in MySQL: mysql> DELETE FROM table WHERE column = value; • Delete specific documents in MongoDB: > db.collection.remove({field:value}) 54 Sunday, March 16, 14
  • 55. Delete (2) • Delete a MySQL database mysql> DROP DATABASE database; • Delete a MongoDB database > use database > db.dropDatabase() 55 Sunday, March 16, 14
  • 57. Export (1) • Export a collection to a JSON file • Ensure mongod is running $ mongoexport --db database -- collection collection --out path/ filename.json 57 Sunday, March 16, 14
  • 58. Export (2) • Export a collection to a CSV file • Ensure mongod is running • A list of fields is required $ mongoexport --db database -- collection collection --fields field1,field2,... --csv --out path/ filename.json 58 Sunday, March 16, 14
  • 59. Import • Import a collection from a JSON, CSV, or TSV file • Ensure mongod is running $ mongoimport --db database -- collection collection < path/ filename.json 59 Sunday, March 16, 14
  • 60. Dump • Dump a specified MySQL database: $ mysqldump -u root --opt database > path.filename.sql • Dump all MongoDB databases: • Ensure mongod is not running $ mongodump --dbpath /data/db --out path 60 Sunday, March 16, 14
  • 62. Package Components (1) • Core Processes • mongod - core DB process • mongos - controller & query router (sharding) • mongo - interactive JavaScript-based shell 62 Sunday, March 16, 14
  • 63. Package Components (2) • Binary Import and Export • mongodump - creates BSON dump files • mongorestore - restores BSON dump files • bsondump - converts BSON to JSON • mongooplog - streams oplog entries 63 Sunday, March 16, 14
  • 64. Package Components (3) • Data Import and Export • mongoimport - imports JSON, CSV, or TSV data formats • mongoexport - exports to JSON, CSV, or TSV data formats 64 Sunday, March 16, 14
  • 65. Package Components (4) • Diagnostic Tools • mongostat - captures database operations by type (insert, query, etc.) • mongotop - tracks read/write activity • mongosniff - provides tracing/sniffing view into database activity • mongoperf - performance testing tool 65 Sunday, March 16, 14
  • 66. Package Components (5) • GridFS • mongofiles - provides a command-line interaction to a GridFS storage system 66 Sunday, March 16, 14
  • 69. Upcoming Events (1) • Trenton Computer Festival • March 14-15, 2014 • tcf-nj.org • Emerging Technologies for the Enterprise • April 22-23, 2014 • phillyemergingtech.com 69 Sunday, March 16, 14