Introduction to NOSQL And MongoDB
-
Upload
behrouz-bakhtiari -
Category
Technology
-
view
3.012 -
download
2
description
Transcript of 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
3
High ThroughputIntroduction to NOSQL And MongoDB
Join
ACID transactions
Reduce CostEliminate ORM Complexity
4
Introduction to NOSQL And MongoDB
Horizontal Scalability
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
6
The CAP-TheoremIntroduction to NOSQL And MongoDB
7
Consistency
Introduction to NOSQL And MongoDB
8
AvailabilityIntroduction to NOSQL And MongoDB
9
Partition ToleranceIntroduction to NOSQL And MongoDB
10
Introduction to NOSQL And MongoDB
Not Available
11
ACIDAtomicConsistentIsolatedDurable
Introduction to NOSQL And MongoDB
12
Introduction to NOSQL And MongoDB
BASEBase Availability
Soft-state
Eventual consistencyNot
durable
13
Introduction to NOSQL And MongoDB
NOSQL TaxonomyKey-Value Stores
Column Stores
Document Stores
Graph Databases
14
Introduction to NOSQL And MongoDB
Key-Value StoresAutomobile
Key Attributes
1 Make: ToyotaModel: HighlanderColor: MaroonYear: 2004
2 Make: ToyotaModel: HighlanderColor: BlueYear: 2005Transmission: Automatic
15
Introduction to NOSQL And MongoDB
Dynamo DBAmazon
Key-Value
Consistent
Hashing
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;
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
18
Introduction to NOSQL And MongoDB
MapReduce
19
Introduction to NOSQL And MongoDB
Graph Databases
20
Introduction to NOSQL And MongoDB
Graph Vs. Key-Value
21
Introduction to NOSQL And MongoDB
JSON
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.
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).
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
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
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)
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
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?
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{
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
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
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 } ] } )
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} } )
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 } ] } )
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
…