2. What we will do today?
- Following Replicated Shard
3. What is MongoDB?
● Document-oriented database
● in MongoDB you store JSON-like documents
with dynamic schemas
● Bridge the gap between key-value stores
and relational databases
5. Why MongoDB?
● Document-oriented
● High performance
● High availability
● Easy scalability
● Rich query language + MapReduce
6. Use Cases
Well Suited
● Archiving and event logging
● Document and Content Management Systems
● ECommerce Often in combination with an RDBMS for the final
order processing and accounting
● Gaming Small read/writes are a good fit for MongoDB
● Mobile Specifically, the server-side infrastructure of mobile
systems. Geospatial.
● Operational data store of a web site MongoDB is very good at
real-time inserts, updates, and queries. Specific web use
case examples:
● content management
● comment storage, management, voting
● user registration, profile, session data
● Real-time stats/analytics
7. What is a Document?
{'id':1, 'category_name' : 'Computer'}
8. Mongo Data Model
Key:
'category_name'
Value:
'Computer'
Field: key-value pair
'category_name':'Computer'
Document: set of fields
{'id':1, 'category_name' : 'Computer'}
9. Mongo Data Model
Collection: set of documents
(say categories)
{'id':1, 'category_name' : 'Computer'},
{'id':2, 'category_name' : 'Mobile'},
...
Database: set of collections
categories
products
members
11. First Run
$ mkdir -p /data/db/ Create a data path
and give permissions
$ chown -R mongod:mongod /data/db/
Start mongod deamon
$ mongod [--dbpath /data/db/] &
$ mongo Connect to mongod
> show dbs
> use admin
> show collections
12. Intro
CRUD Operations
> use testdb
switched to db testdb
> j = { name : "deneme" };
{"name" : "deneme"}
> t = { x : 3 };
{ "x" : 3 }
> db.col.save(j);
> db.col.insert(j); /* see the error message */
> db.col.save(t);
> for (var x = 1; x <= 20; x++) db.col.save({x:x, j:x*x})
>
13. Intro
> db.col.find();
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4f971ab51c3fde3bc55f286f"), "x" : 1, "j" : 1 }
{ "_id" : ObjectId("4f971ab51c3fde3bc55f2870"), "x" : 2, "j" : 4 }
{ "_id" : ObjectId("4f971ab51c3fde3bc55f2871"), "x" : 3, "j" : 9 }
…
has more
> db.col.remove({"x":"3"});
>
16. Master-Slave Rep.
$ mkdir -p /data/db/ms Different data paths
$ mkdir -p /data/db/sl for each instances
$ bin/mongod --master [--port <port>] [--dbpath /data/masterdb/]
$ bin/mongod --slave [--port <port>] --source <masterhostname>[:<port>]
[--dbpath /data/slavedb/]
…
> db.printReplicationInfo() - on master
> db.printSlaveReplicationInfo() - on slave
> use admin
> db.runCommand({resync: 1})
17. ReplicaSet - Voting
Consensus Vote
For a node to be elected primary, it must receive a
majority of votes by the following formula
(floor(5/2)+1)
Arbiters - {_id: 2, host: 'localhost:27019', arbiterOnly:
true}
Reachable Node – heartbeat
Each replica set is limited to 12 total nodes and 7 voting nodes.