This document provides an introduction to NoSQL and MongoDB. It discusses key concepts like horizontal scalability, the CAP theorem, BASE consistency, and MongoDB's document-oriented data model using JSON-like documents and collections. It also covers common MongoDB operations like inserting, querying, updating, and deleting documents.
5. ntroduction to NOSQL And MongoDB
Horizontal Scalability
“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?”
12. ntroduction to NOSQL And MongoDB
BASE
Base Availability
Soft-state
Not
durabl
ual consistency e
13. ntroduction to NOSQL And MongoDB
NOSQL Taxonomy
Key-Value Stores
Column Stores
Document Stores
Graph Databases
14. ntroduction to NOSQL And MongoDB
Key-Value Stores
Automobile
Key Attributes
1 Make: Toyota
Model: Highlander
Color: Maroon
Year: 2004
2 Make: Toyota
Model: Highlander
Color: Blue
Year: 2005
Transmission: Automatic
15. ntroduction to NOSQL And MongoDB
Dynamo DB
Amazon
Key-Value
Consistent
Hashing
16. ntroduction to NOSQL And MongoDB
Column
Stores EmpId Lastname Firstname Salary
1 Smith Joe 40000
2 Jones Mary 50000
3 Johnson Cathy 44000
A Row-oriented Database: puts rows together
1,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. ntroduction to NOSQL And MongoDB
BigTable
Sparse (rows only where there are
values; not fixed length)
Distributed (horizontally partitioned
over many servers)
Multidimensional (consisting of a
multi-part key)
22. ntroduction to NOSQL And MongoDB
Documents
Documents = Rows
In MongoDB the documents are conceptually JSON.
documents can be thought of as objects but only the data of an object, not the code, methods or class hierarchy
MongoDB is type-sensitive and case-sensitive. {“foo” : 3} # {“foo” : “3”} Or {“foo” : 3} # {“Foo” : 3}
23. ntroduction to NOSQL And MongoDB
Collections
Collections = Tables
A MongoDB collection is a collection of BSON documents.
Collections are schema-free.
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. ntroduction to NOSQL And MongoDB
Getting and Starting MongoD
Default port for server
To start the server, run the mongod executable.
Default data directory : C:datadb
If directory.exist(“C:datadb”) == false then server.fail
> mongod --dbpath D:data
25. ntroduction to NOSQL And MongoDB
MongoDB Shell
I want to interact with MongoDB
> mongo
make sure you start mongod before starting the shell.
> X = 200 > Math.sin(Math.PI / 2 );
200 1
>X/5 > “TOSS”.replace(“T”,”Tabriz ”);
40 Tabriz OSS
26. ntroduction to NOSQL And MongoDB
Creating Documents
> db.post.insert({ID: 1,Title:“TDD",Body:"Unit Test"})
> 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"]})
Mapping To Relational Modal
Post PostAuthors Authors
> INSERT INTO Authors ( ID , Name) Values ( 1 , ' Behrouz' )
> INSERT INTO Authors ( ID , Name) Values ( 2 , ' Farid' )
> INSERT INTO post ( ID , Title , Body ) Values ( 3 , ' MongoDB „ , ' Introduction to MongoDB ' )
> INSERT INTO PostAuthors (Post ID , AuthorID) Values ( 3 , 1)
> INSERT INTO PostAuthors (Post ID , AuthorID) Values ( 3 , 2)
27. ntroduction to NOSQL And MongoDB
Deleting Documents
> DELETE FROM post
> db.post.remove()
> DELETE FROM post WHERE ID = 1
> db.post.remove({ ID : 1 })
> DELETE FROM post WHERE ID = 1 AND Title = „TDD‟
> db.post.remove({ ID : 1 , Title : " TDD " })
28. ntroduction to NOSQL And MongoDB
Updating Documents
> 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“ } } )
> db.post.update( { ID: 1 } , { "$set" : {Title : "Mocking“ } } ,false , true)
Upsert ?
29. ntroduction to NOSQL And MongoDB
Updating Documents
> db.post.update( { ID: 1o1 } , { "$set" : { Title : “Scrum“ , Body : “Scrum” } } , true)
Yes
Exist this document Update document
No
Create a new document
{ID : 101}
30. ntroduction to NOSQL And MongoDB
Updating Documents
> db.post.update( {ID : 3 } , { "$set" : { comments : [{ Email : "behrouzlo@yahoo.com" , Body : "Test“ }]}})
Mapping To Relational Modal
Post Comments
1 *
> INSERT INTO comments ( postID , Email , Body ) VALUES ( 3 , ‟behrouzlo@yahoo.com‟ , ‟Test‟ )
31. ntroduction to NOSQL And MongoDB
Querying
> SELECT * FROM post
> db.post.find()
> SELECT ID , Title FROM post
The _id key is always returned, even if it isn‟t specifically listed.
> db.post.find({} , { ID : 1 , Title : 1 })
> db.post.find({} , { ID : 1 , Title : 1 , _id : 0 })
32. ntroduction to NOSQL And MongoDB
Querying
> SELECT * FROM post WHERE ID = 1
> db.post.find({ ID : 1 })
> SELECT * FROM post WHERE ID = 1 OR ID = 3
> db.post.find( { “$or” : [{ ID = 1 } , { ID = 3 } ] } )
33. ntroduction to NOSQL And MongoDB
Querying
$lt < $lte <= $gt > $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. ntroduction to NOSQL And MongoDB
Querying
> SELECT * FROM post WHERE ID = 1
> db.post.find({ ID : 1 })
> SELECT * FROM post WHERE ID = 1 OR ID = 3
> db.post.find( { “$or” : [{ ID = 1 } , { ID = 3 } ] } )
35. ntroduction to NOSQL And MongoDB
Querying
> SELECT * FROM post ORDER BY ID
> db.post.find().sort ( { ID : 1 } )
> SELECT * FROM post ORDER BY ID DESC
> db.post.find().sort ( { ID : -1 } )