36
Introduction to NoSQL And MongoDB 1 Behrouz Bakhtiari Farid Dehgan

Introduction to NOSQL And MongoDB

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to NOSQL And MongoDB

Introduction to NoSQL

And MongoDB

1

Behrouz BakhtiariFarid Dehgan

Page 2: Introduction to NOSQL And MongoDB

Introduction to NOSQL And MongoDB

2

Which ones are correct?

NOSQL = Not To SQL NOSQL = Not Only SQL

1998 Carlo Strozzi

2009 NoSQL meetup in San Farncisco2005 CouchDB

2004 BigTable

2007 MongoDB

1966 IBM IMS1965 MultiV

alue 

Page 3: Introduction to NOSQL And MongoDB

3

High ThroughputIntroduction to NOSQL And MongoDB

Join

ACID transactions

Reduce CostEliminate ORM Complexity

Page 4: Introduction to NOSQL And MongoDB

4

Introduction to NOSQL And MongoDB

Horizontal Scalability

Page 5: Introduction to NOSQL And MongoDB

5

Horizontal Scalability

Data-Dependent Routing

Distributed Partit

ioned Views

Auto

mat

ic Sh

ardi

ng

“Oracle would tell you that with the right degree of hardware and the right configuration of Oracle RAC (Real Application Clusters) and other associated magic software, you can achieve the same scalability. But at what cost?”

“Web 2.0 com

panies can take chances and they need scalability.

When you have these tw

o things in combination, it m

akes [NoSQ

L]

very compelling.”

Introduction to NOSQL And MongoDB

Page 6: Introduction to NOSQL And MongoDB

6

The CAP-TheoremIntroduction to NOSQL And MongoDB

Page 7: Introduction to NOSQL And MongoDB

7

Consistency

Introduction to NOSQL And MongoDB

Page 8: Introduction to NOSQL And MongoDB

8

AvailabilityIntroduction to NOSQL And MongoDB

Page 9: Introduction to NOSQL And MongoDB

9

Partition ToleranceIntroduction to NOSQL And MongoDB

Page 10: Introduction to NOSQL And MongoDB

10

Introduction to NOSQL And MongoDB

Not Available

Page 11: Introduction to NOSQL And MongoDB

11

ACIDAtomicConsistentIsolatedDurable

Introduction to NOSQL And MongoDB

Page 12: Introduction to NOSQL And MongoDB

12

Introduction to NOSQL And MongoDB

BASEBase Availability

Soft-state

Eventual consistencyNot

durable

Page 13: Introduction to NOSQL And MongoDB

13

Introduction to NOSQL And MongoDB

NOSQL TaxonomyKey-Value Stores

Column Stores

Document Stores

Graph Databases

Page 14: Introduction to NOSQL And MongoDB

14

Introduction to NOSQL And MongoDB

Key-Value StoresAutomobile

Key Attributes

1 Make: ToyotaModel: HighlanderColor: MaroonYear: 2004

2 Make: ToyotaModel: HighlanderColor: BlueYear: 2005Transmission: Automatic

Page 15: Introduction to NOSQL And MongoDB

15

Introduction to NOSQL And MongoDB

Dynamo DBAmazon

Key-Value

Consistent

Hashing

Page 16: Introduction to NOSQL And MongoDB

16

Introduction to NOSQL And MongoDB

Column Stores

EmpId Lastname Firstname Salary1 Smith Joe 40000

2 Jones Mary 50000

3 Johnson Cathy 44000

A Row-oriented Database: puts rows together1,Smith,Joe,40000; 2,Jones,Mary,50000; 3,Johnson,Cathy,44000;

A Column-oriented Database: puts values of a column together.1,2,3; Smith,Jones,Johnson; Joe,Mary,Cathy; 40000,50000,44000;

Page 17: Introduction to NOSQL And MongoDB

17

Introduction to NOSQL And MongoDB

Sparse (rows only where there are

values; not fixed length)

Distributed (horizontally partitioned

over many servers)

Multidimensional (consisting of a multi-

part key)

Sorted map (in lexigraphic order)

BigTable

Page 18: Introduction to NOSQL And MongoDB

18

Introduction to NOSQL And MongoDB

MapReduce

Page 19: Introduction to NOSQL And MongoDB

19

Introduction to NOSQL And MongoDB

Graph Databases

Page 20: Introduction to NOSQL And MongoDB

20

Introduction to NOSQL And MongoDB

Graph Vs. Key-Value

Page 21: Introduction to NOSQL And MongoDB

21

Introduction to NOSQL And MongoDB

JSON

Page 22: Introduction to NOSQL And MongoDB

22

Introduction to NOSQL And MongoDB

DocumentsDocuments = Rows

MongoDB is type-sensitive and case-sensitive. {“foo” : 3} # {“foo” : “3”} Or {“foo” : 3} # {“Foo” : 3}

documents can be thought of as objects but only the data of an object, not the code, methods or class hierarchy.

In MongoDB the documents are conceptually JSON.

Page 23: Introduction to NOSQL And MongoDB

23

Introduction to NOSQL And MongoDB

CollectionsCollections = Tables

Collections are schema-free.

A MongoDB collection is a collection of BSON documents.

A collection is created when the first document is inserted.

The maximum size of a collection name is 128 characters (including the name of the db and indexes). 

Page 24: Introduction to NOSQL And MongoDB

24

Introduction to NOSQL And MongoDB

Getting and Starting MongoDBTo start the server, run the mongod executable.

Default port for server

Default data directory : C:\data\db\

If directory.exist(“C:\data\db\”) == false then server.fail

> mongod --dbpath D:\data

Page 25: Introduction to NOSQL And MongoDB

25

Introduction to NOSQL And MongoDB

MongoDB ShellI want to interact with MongoDB

> mongo

make sure you start mongod before starting the shell.

> X = 200200> X / 540

Full-featured JavaScript interpreter

> Math.sin(Math.PI / 2 );1> “TOSS”.replace(“T”,”Tabriz ”);Tabriz OSS

Leverage all of the standard JavaScript libraries

Page 26: Introduction to NOSQL And MongoDB

26

Introduction to NOSQL And MongoDB

Creating Documents> db.post.insert({ID: 2,Title:"NOSQL",Body:"Introduction to NOSQL",Authors:["Farid"]})

> db.post.insert({ID: 3,Title:"MongoDB",Body:"Introduction to MongoDB",Authors:["Farid",“Behrouz"]})

> db.post.insert({ID: 1,Title:“TDD",Body:"Unit Test"})

Post PostAuthors Authors

Mapping To Relational Modal

> INSERT INTO post ( ID , Title , Body ) Values ( 3 , ' MongoDB ‘ , ' Introduction to MongoDB ' )

> INSERT INTO Authors ( ID , Name) Values ( 1 , ' Behrouz' )

> INSERT INTO Authors ( ID , Name) Values ( 2 , ' Farid' )

> INSERT INTO PostAuthors (Post ID , AuthorID) Values ( 3 , 1)

> INSERT INTO PostAuthors (Post ID , AuthorID) Values ( 3 , 2)

Page 27: Introduction to NOSQL And MongoDB

27

Introduction to NOSQL And MongoDB

Deleting Documents> DELETE FROM post

> db.post.remove()

> db.post.remove({ ID : 1 , Title : " TDD " })

> DELETE FROM post WHERE ID = 1 AND Title = ‘TDD’

> db.post.remove({ ID : 1 })

> DELETE FROM post WHERE ID = 1

Page 28: Introduction to NOSQL And MongoDB

28

Introduction to NOSQL And MongoDB

Updating Documents

> db.post.update( { ID: 1 } , { "$set" : {Title : "Mocking“ } } )

> UPDATE post SET Title = ‘Mocking’ Where ID = 1 Updates, update by default only the first documents that matches the criteria.

> db.post.update( { ID: 1 } , { "$set" : {Title : "Mocking“ } } ,false , true)

Upsert?

Page 29: Introduction to NOSQL And MongoDB

29

Introduction to NOSQL And MongoDB

Updating Documents> db.post.update( { ID: 1o1 } , { "$set" : { Title : “Scrum“ , Body : “Scrum” } } , true)

Exist this document Update document

Create a new document

Yes

No

}ID : 101{

Page 30: Introduction to NOSQL And MongoDB

30

Introduction to NOSQL And MongoDB

Updating Documents> db.post.update( {ID : 3 } , { "$set" : { comments : [{ Email : "[email protected]" , Body : "Test“ }]}})

Post Comments

1 *

> INSERT INTO comments ( postID , Email , Body ) VALUES ( 3 , ’[email protected]’ , ’Test’ )

Map

ping

To

Rel

atio

nal M

odal

Page 31: Introduction to NOSQL And MongoDB

31

Introduction to NOSQL And MongoDB

Querying> SELECT * FROM post

> db.post.find()

> SELECT ID , Title FROM post

> db.post.find({} , { ID : 1 , Title : 1 })

The _id key is always returned, even if it isn’t specifically listed.

> db.post.find({} , { ID : 1 , Title : 1 , _id : 0 })

Solution

Page 32: Introduction to NOSQL And MongoDB

32

Introduction to NOSQL And MongoDB

Querying

> SELECT * FROM post WHERE ID = 1 OR ID = 3

> SELECT * FROM post WHERE ID = 1

> db.post.find({ ID : 1 })

> db.post.find( { “$or” : [{ ID = 1 } , { ID = 3 } ] } )

Page 33: Introduction to NOSQL And MongoDB

33

Introduction to NOSQL And MongoDB

Querying$lt < $gt >$lte <= $gte >= $ne <>

> SELECT * FROM post WHERE ID >= 1 And ID <= 100

> db.post.find ( { ID : { “$gte” : 1 , “$lte” : 100 } } )

> SELECT * FROM post WHERE ID <> 1

> db.post.find ( { ID : { “$ne” : 1} } )

Page 34: Introduction to NOSQL And MongoDB

34

Introduction to NOSQL And MongoDB

Querying

> SELECT * FROM post WHERE ID = 1 OR ID = 3

> SELECT * FROM post WHERE ID = 1

> db.post.find({ ID : 1 })

> db.post.find( { “$or” : [{ ID = 1 } , { ID = 3 } ] } )

Page 35: Introduction to NOSQL And MongoDB

35

Introduction to NOSQL And MongoDB

Querying> SELECT * FROM post ORDER BY ID

> db.post.find().sort ( { ID : 1 } )

12

3

100

> SELECT * FROM post ORDER BY ID DESC

> db.post.find().sort ( { ID : -1 } )

1oo

9998

1

Page 36: Introduction to NOSQL And MongoDB

36

Introduction to NOSQL And MongoDB