Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB...

70
1 Getting Started with MongoDB Michael P. Redlich

Transcript of Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB...

Page 1: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

1

Getting Started with MongoDB

Michael P. Redlich

Page 2: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Who’s Mike?

• Bachelor of Science, Computer Science

• “Petrochemical Research Organization”

• Java Queue News Editor, InfoQ

• Leadership Council, Jakarta EE Ambassadors

• Amateur Computer Group of New Jersey

2

Page 3: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Objectives

• What is MongoDB?

• What is NoSQL?

• Getting Started with MongoDB

• Basic CRUD Operations

• Live Demos (yea!)

• MongoDB Resources

3

Page 4: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 5: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 6: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 7: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

How is MongoDB Used?

7

Page 8: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Who is Using MongoDB?

8

Page 9: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 10: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Nomenclature (1)

10

RDBMS MongoDB

Database Database

Table Collection

Row Document

Index Index

Join Embedding & Linking

Foreign Key Reference

Page 11: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Nomenclature (2)

11

Page 12: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 13: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 14: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

What is a Collection?

• A group of documents

• analogous to a table in a RDBMS

• Schema-less

14

Page 15: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 16: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Document Structure{

lastName : “Redlich”,

firstName : “Michael”,

email : “[email protected]

role : {

officer : “President”,

sig : “Java Users Group”

}

}

16

field value

embedded document

Page 17: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Field Names

• Strings

• Cannot contain:

• null

• dots (.)

• dollar sign ($)

• No duplicate field names

17

Page 18: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Conventions Used in This Presentation

• Command Prompt ($)

• MySQL prompt (mysql>)

• MongoDB prompt (>)

• Keywords (db, find(), etc.)

• Variables (variable)

18

Page 19: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Example Database

• ACGNJ Board of Directors:

• lastName

• firstName

• roles (embedded documents)

• tenure

19

Page 20: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Getting Started

• Download MongoDB

• Create a default data directory

•/data/db

•C:\data\db

• Create your first MongoDB database

20

Page 21: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Starting MongoDB

• Start an instance of the MongoDB server:

$ mongod

• Start an instance of the MongoDB client (a JavaScript-based shell):

$ mongo

21

Page 22: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Mongo Shell (1)

• Show the list of shell commands:

> help

• Show the list of databases:

> show dbs

• Show the current database:

> db

22

Page 23: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 24: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Mongo Shell (3)

• Show the recent system.profile entries:

> show profile

• Tab completion

• Command history

24

Page 25: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Primary Key

• Denoted by a special field, _id

• It can be generated:

• Implicitly:

• {_id : ObjectID(value)}

• Explicitly:

• {_id : 2 }, { _id : “MPR”}

25

Page 26: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

ObjectIDs

• Default type for _id

• A 12-byte hexadecimal BSON type:

26

Page 27: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Live Demo!

27

Page 28: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Create

28

Page 29: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Create a Database

• Create a database in MySQL:

mysql> CREATE DATABASE database;

• Create a database in MongoDB:

> use database

29

Page 30: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 31: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Insert Data

• Insert a row in MySQL:

mysql> INSERT INTO table(column,...) VALUES(value,...);

• Insert a document in MongoDB:

> db.collection.insert({field:value,...})

31

Page 32: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 33: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Live Demo!

33

Page 34: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Read

34

Page 35: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Query (1)

• Retrieve all rows in MySQL:

mysql> SELECT * FROM table;

• Retrieve all documents in MongoDB:

> db.collection.find()

35

Page 36: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 37: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 38: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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}).sort({field:1})

38

Page 39: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Query (5)

• Query for multiple documents (returns a cursor):

> db.collection.find()

• Query for one document (returns a single document):

> db.collection.findOne()

39

Page 40: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Query Selectors

• Scalar:

• $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte

• Vector:

• $in, $nin, $all, $size

40

Page 41: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 42: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 43: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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.officer":1})

43

Page 44: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Query (9)

> db.members.find({"roles.director":{$all:["Director"]}})

> db.members.find({"roles.committee":{$in:["Historian","Newsletter"]}})

> db.members.find({roles:{$size:3}})

44

Page 45: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Live Demo!

45

Page 46: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Update

46

Page 47: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 48: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 49: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 50: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Atomic Update Operators

• Scalar:

• $inc, $set, $unset

• Vector:

• $push, $pop, $pull, $pushAll, $pullAll, $addToSet

50

Page 51: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Update (4)

> db.members.update({lastName: "Redlich"},{$set:{"ISODate("2016-12-31")}})

> db.members.update({"roles.sig"},{$set:{"roles.sig":"JUG"}})

51

Page 52: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Delete

52

Page 53: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Delete (1)

• Delete all rows in MySQL:

mysql> DELETE FROM table;

• Delete all documents in MongoDB:

> db.collection.remove()

53

Page 54: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 55: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Delete (2)

• Delete a MySQL database

mysql> DROP DATABASE database;

• Delete a MongoDB database

> use database

> db.dropDatabase()

55

Page 56: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Backup/Restore

56

Page 57: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Export (1)

• Export a collection to a JSON file

• Ensure mongod is running

$ mongoexport --db database --collection collection --out path/filename.json

57

Page 58: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 59: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Import

• Import a collection from a JSON, CSV, or TSV file

• Ensure mongod is running

$ mongoimport --db database --collection collection < path/filename.json

59

Page 60: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 61: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Live Demo!

61

Page 62: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Package Components (1)

• Core Processes

• mongod - core DB process

• mongos - controller & query router (sharding)

• mongo - interactive JavaScript-based shell

62

Page 63: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 64: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 65: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

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

Page 66: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Package Components (5)

• GridFS

• mongofiles - provides a command-line interaction to a GridFS storage system

66

Page 67: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

MongoDB Resources (1)

67

Page 68: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

68

MongoDB Resources (2)

•mongodb.org

•docs.mongodb.org

•mongodb.org/books

•mongodb.com/products/mongodb

•mongodb.com/reference

•bsonspec.org

•education.mongodb.com

Page 69: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

69

Contact Info

[email protected]

@mpredli

redlich.net/portfolio

github.com/mpredli01

Page 70: Getting Started with MongoDB - Redlich · • Getting Started with MongoDB ... RDBMS MongoDB Database Database Table Collection Row Document Index Index Join Embedding & Linking Foreign

Thanks!

70