All about MongoDB
MongoDB
is an open-source, scalable, high-performance document-oriented database.A
document-oriented database or NoSQL database stores data in JSON format rather
than tabular format and allows you to express in the natural form in the way
they meant to be.
Now to understand the NoSQL concept better we can explore and differentiate all types of databases.
- RDBMS
(Relational Database Management System)
- NoSQL
Before
1970, we used to save data in a simple flat-file format and it becomes really
difficult when we need to retrieve any data, there was no standard
implementation.
The
solution to this problem was first defined by EDGAR CODD in mid-1970 as the
relational database. CODD’s View of what quantifies as RDBMS is summarized in
CODD’s 12 rules and then the relational database becomes the predominant type
of database.
Relational
databases are the standard way of storing and querying the data. In the
relational database, we store data in the form of tables E.g.
We
have a class calledStudentwith the properties
|
|
|
The Relational database will save it in a tabular manner
At times it might be possible that certain attributes of the
student do not match with the student class.So here our objects/class may not be uniform and mapping such
objects in a tabular form at times becomes complicated.
Another disadvantage is RDBMS are not capable of handling BIG DATA because of their design and scalability. They are not horizontally scalable.
Another disadvantage is RDBMS are not capable of handling BIG DATA because of their design and scalability. They are not horizontally scalable.
Here comes NoSQL to the rescue. They are horizontally scalable
which a big drawback is in the relational database.
What is horizontal scalability?
Horizontal Scalability is the ability to add and increase the
capacity by adding multiple systems so that they can work as a single unit
without increasing the capacity of a single machine.
Pros and Cons of NoSQL
Pros
· Query language is used in MongoDB
· Performance is much faster
· We can store and querying with BIG
DATA
More advantages of MongoDB
Cons in MongoDB or NoSQL as compared
to RDBMS
· Rolling back of multiple transaction
is not possible
· Joins are not supported.
· Constraints are not implemented at
database level that can be implemented at document level.
Want to explore more about NoSQL please
visit
MongoDB Features
· Support Ad hoc Queries
· Indexing
· Replication
· Duplication of Data
· Load balancing
· Scalability
· Support map-reduce and aggregation tools.
· Uses JavaScript instead of procedure
Ad
hoc Queries
Ad hoc Queries can be updated in real-time which will improve
the overall performance, these are the queries not known while structuring the
database.
Indexing
Any field in the document can be indexed. With the use of
indexes performing any query in MongoDB becomes more efficient.
E.g. if we have hundreds of documents as collections in MongoDB
and we query to find a certain document then MongoDB has to scan the entire
collection to find the documents and if we indexed them then it will be easy
for MongoDB to limit the number of documents to be searched.
E.g. of an index for a collection.
In the above
example studentID and StudentCode is used to index the document so when you
search with studentID the above query will return.
To know more
in detail about indexing with example kindly visit
Replication
It supports Master-Slave Replication that means master can
perform both read and write operations whereas slave can only copy data from
the masters and can only use it for reading and backup. No write operation can
be performed by the slave.
Duplication of Data
MongoDB can run over multiple servers and data is duplication to
keep the system running in case of any hardware failure.
Load Balancing
It has a built-in load balancing configuration because data is
placed in shards in MongoDB.
Scalable
It can be scalable horizontally, new machines can be added to a
running database.
Support Map reduce and aggregation
tools
mapReduce is the data processing mechanism
for condensing large volumes of data into useful aggregated results.MongoDBusesmapReduce command fora mapandreducesoperations.mapReduce is used for processing large data
sets.
Following is the syntax of the basic mapReduce command-
db.collection.mapReduce(
function() {emit(key,value);}, //map
function
function(key,values) {return reduceFunction}, { //reduce function
out: collection,
query: document,
sort: document,
limit: number
}
)
·
mapis
a javascript function that maps a value with a key and emits a key-value pair.
·
reduceis a javascript function that reduces or groups all the
documents having the same key.
·
outspecifies
the location of the map-reduce query result
·
query
·
sortspecifies the optional sort criteria
·
limit;specifies the optional maximum number of documents to be
returned.
Using
MapReduce
Consider the following document
structure.The document stores user_name of the user and the status of post.
{
"post_text": "Skillbakery is an awesome website for tutorials”
"user_name": "Bhawna”
"status":"active"
}
Now, we will use a mapReduce function on ourpostscollection
to select all the active posts, group them on the basis of user_name and then
count the number of posts by each user using the following code –
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
)
The
above mapReduce query outputs the following result –
{
"result" : "post_total",
"timeMillis" : 7,
"counts" : {
"input" : 3
"emit" : 3
"reduce"
: 1,
"output" : 1
},
"ok" : 1,
; }
The result shows that a
total of 3documents matched the query (status: “active"), the map function
emitted 3documents with key-value pairs and finally the reduce function grouped
mapped documents having the same keys into 1.
To see the result of this
mapReduce query, use the find operator –
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
).find()
The above query gives the following result which indicates that both
usersbhawna and kiyansh
has 1 post active
{ "_id" : "bhawna”,”value”
: 1}
{ "_id" : "kiyansh”
,”value” :1}
In a similar manner, MapReduce queries can be used to construct
large complex aggregation queries. The use of custom JavaScript functions make
use of MapReduce which is very flexible and powerful.
Uses JavaScript instead of procedures
JavaScript is used in queries. These queries is transported
from the server
(MongoDB in this case) to the client (the web browser) and
executed on the client without an installation process.
For more information related to MongoDB and if you really
want to master MongoDB this course is good for you
Kindly Visit: SkillBakery Website
For other
such course visit YouTube channel: SkillBakery Studios
No comments:
Post a Comment