2. Agenda
★ MongoDB Recap
★ How mongoDB works?
★ The _id
★ In n Out of Query Execution.
★ Indexes
★ What is Replication?
★ What is Sharding ?
3. About Us
Amit Thakkar
Tech Blogger @
CodeChutney.in
JavaScript Lover
Working on MEAN Stack
Twitter: @amit_thakkar01
LinkedIn: linkedin.com/in/amitthakkar01
Facebook: facebook.com/amit.thakkar01
Vibhor Kukreja
JavaScript Ninja
Working on MEAN Stack
Twitter: @VibhorKukreja
Email : vibhor.kukreja@intelligrape.com
4. MongoDB Recap
1. No-Sql Database.
2. Installation.
3. Basic CRUD operation.
4. Stores data in JSON.
5. Schemaless.
6. Great Performance.
7. No Joins.
8. Easily scalable.
6. How mongoDB works?
There is a process “mongod” that acts as a database server.
This process attaches itself to a directory with the --dbpath option. The default
dbpath is “/data/db”.
And start listening to a specific port number via --port option.The default port is
27017.
> mongod --dbpath ~/testDB --port 28080
You just need to create the testDB directory before allocating it to the
mongod process.
7.
8. Look what’s inside the created dbpath ?
- /journal
- test.ns // namespace for database
- test.0 // raw data storage files
- test.1 // raw data storage files
But what is this journal directory for?
9. Journal Option
Journal option does a write ahead logging to an ondisk
journal to guarantee write operation. i.e write in memory
and on-disk journal before in the data file. ( Data
Consistency )
Where can we set this option?
There is an configuration file for the mongod process.
> cat /etc/mongodb.conf
10. How to get connected with mongod?
There are two modes to interact with the DB.
1. mongoDB Driver/Plugin
- Node.js (mongoose)
- Grails (GORM)
- Python (pymongo)
2. mongo Shell - supports javascript <3
11. mongo Shell
To connect a mongo shell process with our mongod process, we just have to
specify the port number on which the mongod process is listening.
> mongo --port 28080
To check the status of our mongo server, for a proper analysis we must start
the mongostat process to check the server request/response.
> mongostat --port 28080
12. Yeah !! Our Single instance Server is
Up and Running.
At this point of time , we have just created a single
mongoDB server along with a mongo shell that
interacts with this mongoDB server.
13. The _id
It can be represented as a primary key in an collection.
The mongo _id have few properties like,
It must be unique.
It can store any type of data, except boolean and array.
eg - _id : “intelligrape”
_id : { “company” : “intelligrape” }
_id : 17
_id : true //You can do this, but in this case you can have only two records in this collection
By default it stores ObjectId(“HexString”), and it gives a unique value every time as it is computed with
the help of the following.
● The first 4 bytes representing the seconds
● The next 3 bytes are the machine identifier
● The next 2 bytes consists of process id
● The last 3 bytes are a random counter value
14. _id impact on save() and insert() Query
save( ) - is risky :P
If there is an particular insert key already present in the collection, then it will
replace that old document completely from the collection with the new one.
( without even prompting it :D )
So, it may become risky to use save query in our Applications. Its better to use
the insert() query as it throws “duplicate key error index” error.
15. Importing a document in mongoDB
We can easily import a document in mongoDB. Mongo itself provides a process to
import document.
> mongoimport --db test --port 28080 --type json --collection person < testColl.json
--db defines the database name
--port server port number
--type document type, which is to be import
--collection name of the collection, where insertion is to be done
16. PowerOf2Sizes ( v2.6 +)
What is move and padding in mongoDB storage?
> db.runCommand({ collMod : ”collection_Name”,usePowerOf2Sizes : true })
17. In n Out of Query Execution.
> db.person.find({}).sort({name:1}).skip(2).limit(3);
> db.person.find({}).skip(2).sort({name:1}).limit(3);
> db.person.find({}).limit(3).skip(2).sort({name:1});
What impact does they will make, on the result?
18.
19. Bulk Write Operations ( v2.6+)
Type of bulk operations -
● Ordered
● Unordered
> var bulk = db.person.initializeUnorderedBulkOp();
> bulk.insert( { name : ”Joe” } );
> bulk.insert( { name : ”Tim” } );
> bulk.insert( { name : ”Steve” } );
> bulk.execute();
20. What is an Index?
A data structure that can be used to make certain
queries more efficient.
21.
22. How can we index ?
An index on _id is automatic. For more, ensureIndex :
> db.posts.ensureIndex({“name”:1});
> db.posts.ensureIndex({name: 1, date: ‐1});
> db.posts.ensureIndex({title: 1},{unique: true, sparse:true,dropDups:true});
> db.posts.ensureIndex(...,{background: true});
> db.posts.ensureIndex({“comments.author”: 1});
{“tags”: [“mongodb”, “indexing”], ...}
> db.posts.ensureIndex({“tags”: 1}); // multikey
> db.posts.ensureIndex({“location”: “2d”}); //geospatial
db.post.find({title:”mongo”}).hint({name:1,title:1});
23. What is Replication?
● Replication is the process of synchronizing data across
multiple servers.
● Mongo achieves Replication through Replica Sets
● Replica sets are a form of asynchronous master/slave
replication
● A replica set consists of two or more nodes that are
copies of each other. (i.e.: replicas)
24.
25. Purpose of Replication
● Automated Failover / High Availability
If primary fails then replica set will
attempt to select another member
to become the new primary.
Use heartbeat signal to detect failure
26. ● Distributed Read Load/Read Scaling
By default, the primary node of a replica set is accessed for
all reads and writes.
● Disaster Recovery
So, you have a pool of servers with one primary (the
master) and N secondaries (slaves). If the primarycrashes
or disappears, the other servers will hold an election to
choose a new primary. Arbiter helps to achieve >50 votes
in case of draw.
connectionString = (“mongodb://localhost:37017", replicaSet="s0",w=4,
j=True)
27. Lets Create a Replica Set :P
mkdir -p ~/tempDB/rs1 ~/tempDB/rs2 ~/tempDB/rs3
mongod --replSet m101 --logpath "1.log" --dbpath ~/tempDB/rs1 --port
47017 --fork
mongod --replSet m101 --logpath "2.log" --dbpath ~/tempDB/rs2 --port
47018 --fork
mongod --replSet m101 --logpath "3.log" --dbpath ~/tempDB/rs3 --port
47019 --fork
Seperate mongod processes have been created, lets connect them with
each other.