Getting Started with MongoDB (TCF ITPC 2014)

Michael Redlich
Michael RedlichSystems Analyst, Polymer Physicist at Amateur Computer Group of New Jersey
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
1 of 71

Recommended

MongoDB at GUL by
MongoDB at GULMongoDB at GUL
MongoDB at GULIsrael Gutiérrez
424 views39 slides
An Overview of Data Management Paradigms: Relational, Document, and Graph by
An Overview of Data Management Paradigms: Relational, Document, and GraphAn Overview of Data Management Paradigms: Relational, Document, and Graph
An Overview of Data Management Paradigms: Relational, Document, and GraphMarko Rodriguez
8.1K views37 slides
MongoDB by
MongoDBMongoDB
MongoDBBembeng Arifin
503 views31 slides
Mongo db basics by
Mongo db basicsMongo db basics
Mongo db basicsHarischandra M K
2.1K views66 slides
Using MongoDB and Python by
Using MongoDB and PythonUsing MongoDB and Python
Using MongoDB and PythonMike Bright
1.5K views38 slides
Introduction to MongoDB by
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
1.3K views21 slides

More Related Content

What's hot

2011 Mongo FR - MongoDB introduction by
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
1.1K views21 slides
Mongo db by
Mongo dbMongo db
Mongo dbRaghu nath
870 views25 slides
Chen li asterix db: 大数据处理开源平台 by
Chen li asterix db: 大数据处理开源平台Chen li asterix db: 大数据处理开源平台
Chen li asterix db: 大数据处理开源平台jins0618
378 views32 slides
Mongo DB 102 by
Mongo DB 102Mongo DB 102
Mongo DB 102Abhijeet Vaikar
1.1K views13 slides
Mongo db by
Mongo dbMongo db
Mongo dbNoman Ellahi
220 views18 slides
Introduction to MongoDB by
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNosh Petigara
801 views24 slides

What's hot(20)

2011 Mongo FR - MongoDB introduction by antoinegirbal
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
antoinegirbal1.1K views
Chen li asterix db: 大数据处理开源平台 by jins0618
Chen li asterix db: 大数据处理开源平台Chen li asterix db: 大数据处理开源平台
Chen li asterix db: 大数据处理开源平台
jins0618378 views
2011 mongo FR - scaling with mongodb by antoinegirbal
2011 mongo FR - scaling with mongodb2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb
antoinegirbal702 views
Intro To MongoDB by Alex Sharp
Intro To MongoDBIntro To MongoDB
Intro To MongoDB
Alex Sharp93.1K views
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes by MongoDB
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB5.5K views
Get expertise with mongo db by Amit Thakkar
Get expertise with mongo dbGet expertise with mongo db
Get expertise with mongo db
Amit Thakkar1K views
JSONpedia - Facilitating consumption of MediaWiki content by Michele Mostarda
JSONpedia - Facilitating consumption of MediaWiki contentJSONpedia - Facilitating consumption of MediaWiki content
JSONpedia - Facilitating consumption of MediaWiki content
Michele Mostarda3.5K views
Introduction to mongoDB by Kangaroot
Introduction to mongoDBIntroduction to mongoDB
Introduction to mongoDB
Kangaroot621 views
Back to Basics, webinar 2: La tua prima applicazione MongoDB by MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBBack to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDB
MongoDB1.2K views
5 Pitfalls to Avoid with MongoDB by Tim Callaghan
5 Pitfalls to Avoid with MongoDB5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB
Tim Callaghan6.1K views
MongoDB basics & Introduction by Jerwin Roy
MongoDB basics & IntroductionMongoDB basics & Introduction
MongoDB basics & Introduction
Jerwin Roy544 views
Connecting NodeJS & MongoDB by Enoch Joshua
Connecting NodeJS & MongoDBConnecting NodeJS & MongoDB
Connecting NodeJS & MongoDB
Enoch Joshua242 views

Similar to Getting Started with MongoDB (TCF ITPC 2014)

Getting Started with MongoDB by
Getting Started with MongoDBGetting Started with MongoDB
Getting Started with MongoDBMichael Redlich
2.1K views71 slides
MongoDB: a gentle, friendly overview by
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
2.3K views29 slides
Mongodb intro by
Mongodb introMongodb intro
Mongodb introchristkv
4.1K views37 slides
Starting with MongoDB by
Starting with MongoDBStarting with MongoDB
Starting with MongoDBDoThinger
621 views18 slides
Mongo db php_shaken_not_stirred_joomlafrappe by
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeSpyros Passas
1.2K views36 slides
Getting Started with C++ (TCF 2014) by
Getting Started with C++ (TCF 2014)Getting Started with C++ (TCF 2014)
Getting Started with C++ (TCF 2014)Michael Redlich
1.3K views38 slides

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

MongoDB: a gentle, friendly overview by Antonio Pintus
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
Antonio Pintus2.3K views
Mongodb intro by christkv
Mongodb introMongodb intro
Mongodb intro
christkv4.1K views
Starting with MongoDB by DoThinger
Starting with MongoDBStarting with MongoDB
Starting with MongoDB
DoThinger621 views
Mongo db php_shaken_not_stirred_joomlafrappe by Spyros Passas
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappe
Spyros Passas1.2K views
Getting Started with C++ (TCF 2014) by Michael Redlich
Getting Started with C++ (TCF 2014)Getting Started with C++ (TCF 2014)
Getting Started with C++ (TCF 2014)
Michael Redlich1.3K views
OSDC 2012 | Building a first application on MongoDB by Ross Lawley by NETWAYS
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
NETWAYS22 views
MongoDB Strange Loop 2009 by Mike Dirolf
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
Mike Dirolf1.7K views
Indexing Strategies to Help You Scale by MongoDB
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
MongoDB6.5K views
How to use MongoDB with CakePHP by ichikaway
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
ichikaway16.5K views
MongoDB at CodeMash 2.0.1.0 by Mike Dirolf
MongoDB at CodeMash 2.0.1.0MongoDB at CodeMash 2.0.1.0
MongoDB at CodeMash 2.0.1.0
Mike Dirolf4.8K views
MongoDB NYC Python by Mike Dirolf
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC Python
Mike Dirolf877 views
MongoDB for Coder Training (Coding Serbia 2013) by Uwe Printz
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz4.2K views
RethinkDB - the open-source database for the realtime web by Alex Ivanov
RethinkDB - the open-source database for the realtime webRethinkDB - the open-source database for the realtime web
RethinkDB - the open-source database for the realtime web
Alex Ivanov812 views
Basics of MongoDB by Habilelabs
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
Habilelabs2.6K views

More from Michael Redlich

Getting Started with GitHub by
Getting Started with GitHubGetting Started with GitHub
Getting Started with GitHubMichael Redlich
153 views30 slides
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework by
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based FrameworkMichael Redlich
415 views55 slides
Building Microservices with Helidon: Oracle's New Java Microservices Framework by
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices FrameworkMichael Redlich
274 views58 slides
Introduction to Object Oriented Programming & Design Principles by
Introduction to Object Oriented Programming & Design PrinciplesIntroduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design PrinciplesMichael Redlich
377 views59 slides
Getting Started with C++ by
Getting Started with C++Getting Started with C++
Getting Started with C++Michael Redlich
249 views39 slides
C++ Advanced Features by
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced FeaturesMichael Redlich
280 views62 slides

More from Michael Redlich(20)

Building Microservices with Micronaut: A Full-Stack JVM-Based Framework by Michael Redlich
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Michael Redlich415 views
Building Microservices with Helidon: Oracle's New Java Microservices Framework by Michael Redlich
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices Framework
Michael Redlich274 views
Introduction to Object Oriented Programming & Design Principles by Michael Redlich
Introduction to Object Oriented Programming & Design PrinciplesIntroduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design Principles
Michael Redlich377 views
Introduction to Object Oriented Programming &amp; Design Principles by Michael Redlich
Introduction to Object Oriented Programming &amp; Design PrinciplesIntroduction to Object Oriented Programming &amp; Design Principles
Introduction to Object Oriented Programming &amp; Design Principles
Michael Redlich117 views
Building Realtime Access to Data Apps with jOOQ by Michael Redlich
Building Realtime Access to Data Apps with jOOQBuilding Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQ
Michael Redlich225 views
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017) by Michael Redlich
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Michael Redlich281 views
Building Realtime Web Apps with Angular and Meteor by Michael Redlich
Building Realtime Web Apps with Angular and MeteorBuilding Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and Meteor
Michael Redlich319 views
Java Advanced Features (TCF 2014) by Michael Redlich
Java Advanced Features (TCF 2014)Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)
Michael Redlich754 views
Introduction to Object-Oriented Programming & Design Principles (TCF 2014) by Michael Redlich
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Michael Redlich2.4K views
Getting Started with Meteor (TCF ITPC 2014) by Michael Redlich
Getting Started with Meteor (TCF ITPC 2014)Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)
Michael Redlich902 views
Getting Started with Java (TCF 2014) by Michael Redlich
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)
Michael Redlich777 views
C++ Advanced Features (TCF 2014) by Michael Redlich
C++ Advanced Features (TCF 2014)C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)
Michael Redlich1.4K views

Recently uploaded

[2023] Putting the R! in R&D.pdf by
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdfEleanor McHugh
38 views127 slides
Top 10 Strategic Technologies in 2024: AI and Automation by
Top 10 Strategic Technologies in 2024: AI and AutomationTop 10 Strategic Technologies in 2024: AI and Automation
Top 10 Strategic Technologies in 2024: AI and AutomationAutomationEdge Technologies
14 views14 slides
Data-centric AI and the convergence of data and model engineering: opportunit... by
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...Paolo Missier
34 views40 slides
Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
50 views21 slides
The Research Portal of Catalonia: Growing more (information) & more (services) by
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)CSUC - Consorci de Serveis Universitaris de Catalunya
73 views25 slides
Roadmap to Become Experts.pptx by
Roadmap to Become Experts.pptxRoadmap to Become Experts.pptx
Roadmap to Become Experts.pptxdscwidyatamanew
11 views45 slides

Recently uploaded(20)

[2023] Putting the R! in R&D.pdf by Eleanor McHugh
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf
Eleanor McHugh38 views
Data-centric AI and the convergence of data and model engineering: opportunit... by Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier34 views
Emerging & Future Technology - How to Prepare for the Next 10 Years of Radica... by NUS-ISS
Emerging & Future Technology - How to Prepare for the Next 10 Years of Radica...Emerging & Future Technology - How to Prepare for the Next 10 Years of Radica...
Emerging & Future Technology - How to Prepare for the Next 10 Years of Radica...
NUS-ISS16 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
Future of Learning - Yap Aye Wee.pdf by NUS-ISS
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdf
NUS-ISS41 views
Empathic Computing: Delivering the Potential of the Metaverse by Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst470 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman27 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin75 views
RADIUS-Omnichannel Interaction System by RADIUS
RADIUS-Omnichannel Interaction SystemRADIUS-Omnichannel Interaction System
RADIUS-Omnichannel Interaction System
RADIUS15 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab15 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada121 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs169 views

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