5. Challenges with RDBMS
Assumes
WELL DEFINED data structure
data is DENSE and largely UNIFORM
PROPERTIES of data can be defined upfront
INTERRELATIONSHIP are well ESTABLISHED & SYSTEMATIC
INDEXES can be CONSISTENTLY DEFINED
May SNAP when data grows enormously
But ...
that may not be TRUE
5
6. DATA is
Unstructured
Does not make sense
OW
DATA is GR IN G at high speed
6
7. NoSQL?
No Rel / No SQL / Not Only SQL / NoSQL
Non RELATIONAL
DISTRIBUTED DATASTORES
May NOT provide ACID
Not a Single Product or Technology
Data Storage and Manipulation
7
8. NoSQL Storage Types
DOCUMENT STORE
GRAPH
KEY-VALUE STORE
TABULAR
multivalue database
object database
rdf database
tuple store
8
11. Best Features of KEY-VALUE stores,
DOCUMENT database & RELATIONAL
databases
11
12. Why so POPULAR?
EASY to USE
SIMPLER concepts
EASY on ramp for NoSQL
Many DEV/OPERATIONAL things come inbuilt
More AGILE than RDBMS
Less UPFRONT DESIGN needed than RDBMS
SCALABLE schema and storage
12
13. Built for SPEED
Built for SPEED (written in C++)
Data serialized to BSON
Extensive use of MEMORY MAPPED Files
PADS DISK SPACE around document
13
15. Negatives of MONGODB
INDEXES are not flexible
REALTIME QUERIES may not be as fast as others
Good enough only if queries are SIMPLE
Not as MATURED as RDBMS
15
22. MySQL to MongoDB
MySQL Term MongoDB Term
Database Database
Tables Collection
Rows BSON Document
Column BSON Fields
Index Index
Join Embedding and Linking
Primary Key _id field
Group by Aggregation
22
23. SQL to MONGO
SQL Statements Mongo Statements
CREATE DATABASE sqltest use mongotest
USE sqltest implicit
CREATE TABLE table1 (a Number, b db.createCollection('coll1');
Number)
ALTER TABLE table1 add … implicit
INSERT INTO table1 values(100,22) db.coll1.insert({'a':'100,'b':22});
23
24. SQL to MONGO
SQL Statements Mongo Statements
SELECT * from table1 db.coll1.find();
SELECT * from table1 where a=33 db.coll1.find({a:33});
SELECT a,b from table1 where a =44 db.coll1.find({a:44},{a:1,b:1});
SELECT * from table1 where b like '%abc%' db.coll1.find({b:/abc/});
SELECT * from table1 where b like 'abc%' db.coll1.find({b:/^abc/});
SELECT * from table1 where a>50 db.coll1.find({a:{$gt:50}});
SELECT * from table1 where a <40 db.coll1.find({a:{$lt:40}});
SELECT * from table1 where a<=40 db.coll1.find({a:{$lte:40}});
SELECT * from table1 where a =33 order by db.coll1.find({a:33}).sort({name:1})
name
SELECT * from table1 where a=33 order by db.coll1.find({a:33}).sort({name:-1})
name DESC
SELECT * from table1 LIMIT 1 db.coll1.findOne();
24
25. SQL to MONGO
SQL Statements Mongo Statements
SELECT * from table1 where a=33 and b=100 db.coll1.find({a:33,b:100});
SELECT * from table1 LIMIT 100 db.coll1.find().limit(100);
SELECT * from table1 where a =33 or a=45 or db.coll1.find({$or:[{a:33},{a:45},{a:50}]})
a=50
SELECT DISTINCT a from table1 db.coll1.distinct('a');
SELECT count(*) from table1 db.coll1.count();
SELECT count(*) from table1 where a=33 db.coll1.find({a:33}).count();
CREATE INDEX myindexname ON table1(a) db.coll1.ensureIndex({a:1})
UPDATE table1 set a=44 where b=100 db.coll1.update({b:100},{$set:
{a:44}},false,true);
DELETE from table1 where a=44 db.coll1.remove({a:44});
25
26. INSTALLATION
It is DEAD SIMPLE
Refer to the following link
http://www.mongodb.org/display/DOCS/Quickstart/
26
32. Inserting DOCUMENT
db.collection.insert(<json formatted data>);
e.g.
db.articles.insert({title:'This is test article',slug:'this-is-test-article',text:'This is test article
and it does not contain anything',count:10});
32
34. Updating DOCUMENTS
db.collection.update( criteria, objNew, upsert, multi )
Upsert = update data if exists, insert as new data if not
Multi = make multiple updates matching criteria
e.g.
db.articles.update({count:20},{$set:{title:'This is second article',slug:'this-is-second-
art'}},true,false)
34
35. creating INDEX
db.collection.ensureIndex(index);
eg.
db.articles.ensureIndex({title:1})
35
36. MONGODB-PHP EXTENSION
Available from PECL
shell> pecl install mongo
36
37. Loading MONGO EXTENSION in PHP
Add extension=mongo.so to php.ini
Or any other file like mongo.ini but make sure it is loaded by php
37