Вы находитесь на странице: 1из 61

MongoDB

Introduction to MongoDB

MongoDB
NoSQL
NoSQL is a new way of designing Internet-scale database solutions.
It is not a product or technology but a term that defines a set of database technologies
that are not based on the traditional RDBMS principles.
SQL
The idea of RDBMS was borne from E.F. Codds 1970 whitepaper titled A relational
model of data for large data
The language used to query RDBMS systems is SQL (Sequel Query Language ).
RDBMS systems are well suited for structured data held in columns and rows,
The RDBMS systems are based on the concept of ACID transactions. ACID stands for
Atomic, Consistent, Isolated, and Durable, where

MongoDB

Advantages of NoSQL
High scalability NoSql databases are expand horizontally using low-end
commodity servers
Manageability and administration : NoSQL databases are designed to mostly
work with automated repairs, distributed data, and simpler data models, leading to
low manageability and administration
Low cost : NoSQL databases are typically designed to work with a cluster of
cheap commodity servers, enabling the users to store and process more data at a
low cost.
Flexible data models : NoSQL databases have a very flexible data model,
enabling them to work with any type of data;

MongoDB

Disadvantages of NoSQL
Maturity : Most NoSQL databases are pre-production
versions with key features that are still to be implemented
Support : Support is one limitation that you need to
consider. support is very minimal as compared to the
enterprise software companies
Limited Query Capabilities : Since NoSQL databases are
generally developed to meet the scaling requirement of
the web-scale applications, they provide limited querying
capabilities
Expertise : Since NoSQL is an evolving area, expertise on
the technology is limited in the developer and
administrator community

MongoDB

MongoDB

NoSQL Category

MongoDB

Feature Comparison

MongoDB

MongoDB is a cross-platform, document oriented database that


provides, high performance, high availability, and easy
scalability. MongoDB works on concept of collection and
document
MongoDB is a database management system designed to
rapidly develop web applications and internet infrastructure
The data model and persistence strategies are built for high
read-and-write throughput and the ability to scale easily with
automatic failover.
8/2/16

MongoDB

Non Relational approached


Traditional RDBMS platforms provide scalability using a
scale-up
approach, which requires a faster server to increase
performance. The following issues led to why MongoDB
and other NoSQL databases were designed
A document-based data model can represent rich,
hierarchical
data structures. Its often possible to do without the
multitable joins
MongoDB uses a JSON-based (JavaScript Object Notation)
document store for the data. JSON/BSON offers a schema-less
model, which provides flexibility in terms of database design.
Unlike in RDBMSs, changes can be done to the schema

MongoDB

Performance and features


MongoDB is a document-oriented DBMS where data is stored as
documents no joins are required so data retrieval is fast.
MongoDB does not have fully generalized transactions
MongoDB provide support for secondary indexes, it enables users
to query using query documents
MongoDB provides a replica set, which is a form of master-slave
replication with automated failover, and it has built-in horizontal
scaling.

MongoDB

Running the Database Anywhere


MongoDB can run on Server, VM, Cloud pey per for what you use service
The language used for implementing MongoDB is C++, which enables MongoDB to
achieve this goal
The 10gen site provides binaries for different OS platforms, enabling MongoDB to run on
almost any type of machine.

MongoDB

MongoDB Data Model

MongoDB

Database, Collection & Document


Database
Database is a physical container for collections. Each database gets its own set of
files on the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table.
A collection exists within a single database. Collections do not enforce a schema.
Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents n the same collection do not need to have the same
set of fields or structure, and common fields in a collection's documents may hold
different types of data

MongoDB
MongoDB stores its information in documents rather than rows.
{
_id: 10,
username: 'peter',
email: 'pbbakkum@gmail.com'
}
Consider the case where youd
like to store multiple emails for each user. In the relational world, you might create
a separate table of email addresses and the users to which theyre associated
{
_id: 10,
username: 'peter',
email: [
'pbbakkum@gmail.com', 'pbb7c@virginia.edu'
]
}

MongoDB

JSON and BSON


MongoDB is a document-based database. It uses Binary JSON
for storing its data.

MongoDB
MongoDBs document format is based on JSON, a popular scheme for storing
arbitrary
data structures JSON structures consist of keys and values,

MongoDB
Advantages of MongoDB over RDBMS
Schema less : MongoDB is document database in which one collection holds
different different documents. Number of fields, content and size of the
document can be differ from one document to another.
Structure of a single object is clear
No complex joins
Deep query-ability. MongoDB supports dynamic queries on documents using a
document-based query language that's nearly as powerful as SQL
Tuning
Ease of scale-out: MongoDB is easy to scale
Conversion / mapping of application objects to database objects not needed
Uses internal memory for storing the (windowed) working set, enabling faster
access of data

MongoDB

Why should use MongoDB


Document Oriented Storage : Data is stored in the form of JSON style
documents
Index on any attribute
Replication & High Availability
Auto-Sharding
Rich Queries
Fast In-Place Updates
Professional Support By MongoDB

MongoDB

Where should use MongoDB?


Big Data
Content Management and Delivery
Mobile and Social Infrastructure
User Data Management
Data Hub

MongoDB

MongoDB Data Modelling


Design your schema according to user requirements.
Combine objects into one document if you will use them together. Otherwise
separate them (but make sure there
should not be need of joins).
Duplicate the data (but limited) because disk space is cheap as compare to compute
time.
Do joins while write, not on read.
Optimize your schema for most frequent use cases.
Do complex aggregation in the schema

MongoDB

Examples
Suppose a client needs a database design for his blog website and see the
differences between RDBMS and MongoDB schema design. Website has the
following requirements.
Every post has the unique title, description and url.
Every post can have one or more tags.
Every post has the name of its publisher and total number of likes.
Every Post have comments given by users along with their name, message, datatime and likes.
On each post there can be zero or more comments.
In RDBMS schema design for above requirements will have minimum three tables

MongoDB

MongoDB

MongoDB

Starting the shell


Mongo
> x = 200
200
> x / 5;
We can also leverage all of the standard JavaScript libraries:
> Math.sin(Math.PI / 2);
1
> new Date("2010/1/1");
"Fri Jan 01 2010 00:00:00 GMT-0500 (EST)
> "Hello, World!".replace("World", "MongoDB");
Hello, MongoDB!

MongoDB

co
We can even define and call JavaScript functions:
> function factorial (n) {
... if (n <= 1) return 1;
... return n * factorial(n - 1);
... }
> factorial(5);
120
To see the database db is currently assigned to, type in db and hit Enter:
> db
Test
db.stats()
Show dbs
Show collections

MongoDB

Query & insert the data


> use tutorial
switched to db tutorial
> db.users.insert({username: "smith"})
db.users.find()
Inserts and queries
> db.users.insert({username: "smith"})
> db.users.find()
> db.users.insert({username: "jones"})
> db.users.count()
2

MongoDB

Query the data


> db.users.find()
> db.users.find({username: "jones"})
> db.users.find({
... _id: ObjectId("552e458158cd52bcb257c324"),
... Username
: "smith"
... })
> db.users.find({ $and: [
... { _id: ObjectId("552e458158cd52bcb257c324") },
... { username: "smith" }
... ] })
> db.users.find({ $or: [
... { username: "smith" },
... { username: "jones" }
... ]})

MongoDB

Update the data


> db.users.update({username: "smith"}, {$set: {country: "Canada"}})
> db.users.find({username: "smith"})
> db.users.update({username: "smith"}, {country: "Canada"})
> db.users.update({username: "smith"}, {$unset: {country: 1}})

MongoDB

Update complex data


db. player.users.update( {username: babjee"},
{
$set: {
favorites: {
cities: ["Chicago", "Cheyenne"],
movies: ["Casablanca", "For a Few Dollars More", "The Sting"]
}
}
})

db.users.update( {username: "jones"},


{
$set: {
favorites: {
movies: ["Casablanca", "Rocky"]
}
}
})

MongoDB

Delete the records


> db.users.find({"favorites.movies": "Casablanca"})
> db.users.update( {"favorites.movies": "Casablanca"},
{$addToSet: {"favorites.movies": "The Maltese Falcon"} },
alse,
true )
> db.foo.remove()

db.users.remove({"favorites.cities": "Cheyenne"})
> db.users.drop()

MongoDB

Create collection
db.createCollection("mycollection")

Following example shows the syntax of createCollection()


method with few important options:
db.createCollection("mycol", { capped : true, autoIndexID :
true, size : 6142800, max : 10000 } )
o
Db.dbname.dropcollection()

MongoDB
db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],likes: 100},
{
title: 'NoSQL Database',
description: 'NoSQL database doesnt have tables',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment,dateCreated: new Date(2013,11,10,2,35),
like: 0 } ]
}
])

MongoDB

Save metho
To insert the document you can use
db.post.save(document) also.

If you don't specify _id in the document then save()


method will work same as insert() method. If you specify
_id then it will replace whole data of document
containing _id as specified in save() method

MongoDB

Find method
find() method will display all the documents in a non
structured way.
The pretty() Method To display the results in a formatted
way, you can use pretty() method.
findOne() method, that reruns only one document.

MongoDB

MongoDB

AND /OR
db.mycol.find({key1:value1, key2:value2}).pretty()
db.mycol.find({"by":"tutorials point","title": "MongoDB
Overview"}).pretty()
db.mycol.find({$and: [
{key1: value1}, {key2:value2}
]
}

MongoDB

db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
db.mycol.find({$or:[{"by":"tutorials point"},{"title":
"MongoDBOverview"}]}).pretty()

MongoDB

MongoDB Projection
The find() Method
MongoDB's find() method, Query Document accepts
second optional parameter that is list of fields that you
want to retrieve. In MongoDB when you execute find()
method, then it displays all fields of a document.
To limit this you need to set list of fields with value 1 or 0.
1 is used to show the filed while 0 is used to hide the field.
>db.mycol.find({},{"title":1,_id:0})

MongoDB

The Limit() Method


To limit the records in MongoDB, you need to use limit()
method. limit() method accepts one number type
argument, which is number of documents that you want to
displayed
db.mycol.find({},{"title":1,_id:0}).limit(2)
MongoDB Skip() Method
db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)

MongoDB

MongoDB Sort Documents


To sort documents in MongoDB, you need to use sort()
method. sort() method accepts a document containing
list of fields along with their sorting order. To specify
sorting order 1 and -1 are used. 1 is used for ascending
order while -1 is used for descending order

db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})

MongoDB

MongoDB Indexing
Indexes support the efficient resolution of queries. Without
indexes, MongoDB must scan every document of a collection
to select those documents that match the query statement
The index stores the value of a specific field or set of fields,
ordered by the value of the field as specified in index.
The ensureIndex() Method
To create an index you need to use ensureIndex() method of
mongodb
db.COLLECTION_NAME.ensureIndex({KEY:1})

MongoDB

db.mycol.ensureIndex({"title":1}

db.mycol.ensureIndex({"title":-1})

ensureIndex() method also accepts list of options (which


are optional), whose list is given below:

MongoDB

MongoDB

Creating large collection


for(i = 0; i < 20000; i++) {
db.numbers.save({num: i});
}
db.numbers.count()
db.numbers.find({num: 500})
db.numbers.find( {num: {"$gt": 19995 }} )
db.numbers.find( {num: {"$gt": 20, "$lt": 25 }} )
db.numbers.find({num: {"$gt": 19995}}).explain("executionStats")

MongoDB

db.numbers.createIndex({num: 1})
db.numbers.getIndexes()

db.numbers.find({num: {"$gt":
19995 }}).explain("executionStats")

db.post.find({'comments.user':/^us/})

MongoDB

Import & Export


mongoimport --db users --collection contacts --type csv
--headerline --file c:\user.csv

mongoexport --db users --collection contacts --type=csv


--fields name,address --out /opt/backups/contacts.csv

MongoDB

Aggregation Frame work


The MongoDB aggregation framework, first introduced in
MongoDB v2.2, has continued to evolve over subsequent
releases
A call to the aggregation framework the aggregation
pipeline, where the output from each step in the pipeline
provides input to the next step
Aggregation pipeline operations include the following:

MongoDB

$projectSpecify fields to be placed in the output document (projected).


$matchSelect documents to be processed, similar to find()
$limitLimit the number of documents to be passed to the next step.
$skipSkip a specified number of documents.
$unwindExpand an array, generating one output document for each
array entry.
$groupGroup documents by a specified key.
$sortSort documents.
$geoNearSelect documents near a geospatial location.
$outWrite the results of the pipeline to a collection (new in v2.6).
$redactControl access to certain data (new in v2.6)..

MongoDB

Piple line aggrigation


db.products.aggregate([ {$match: }, {$group: }, {$sort: } ] )

The entire products collection is passed to the $match operation, which then
selects only certain documents from the input collection.
The output from $match is passed to the $group operator, which then groups
the output by a specific key to provide new information such as sums and
averages.
The output from the $group operator is then passed to a final $sort operator
to be sorted before being returned as the final result.

MongoDB

Sql versus aggregation framework comparsion

MongoDB

db.mycol.aggregate([{$group : {_id : "$by_user",


num_tutorial : {$sum : 1}}}])
db.mycol.aggregate([{$group : {_id: "$by_user",
num_tutorial : {$sum :"$likes"}}}])
db.mycol.aggregate([{$group : {_id : "$by_user",
num_tutorial : {$avg :"$likes"}}}])
db.mycol.aggregate([{$group : {_id : "$by_user",
num_tutorial : {$min :"$likes"}}}])

MongoDB

db.mycol.aggregate([{$group : {_id: "$by_user", max_val :


{$max :"$likes"}}}])
Push :Inserts the value to an array in the resulting document
db.mycol.aggregate([{$group : {_id : "$by_user", url :
{$push: "$url"}}}])
db.mycol.aggregate([{$group : {_id : "$by_user", first_url :
{$first : "$url"}}}])
db.mycol.aggregate([{$group : {_id: "$by_user", last_url :
{$last :"$url"}}}])

MongoDB

MongoDB Replication
Replication is the process of synchronizing data across multiple servers
Replication provides redundancy and increases data availability with
multiple copies of data on different database servers
replication protects a database from the loss of a single server
Replication also allows you to recover from hardware failure and service
Interruptions
With additional copies of the data, you can dedicate one to disaster
recovery, reporting, or backup.

MongoDB

How replication works in MongoDB


MongoDB achieves replication by the use of replica set.
Replica set is a group of mongod instances that host the
same data set.
In a replica one node is primary node that receives all write
Operations
All other instances, secondaries, apply operations from the
primary so that they have the same data set

MongoDB

How replication works in MongoDB


1. Replica set is a group of two or more nodes (generally
minimum 3 nodes are required).
2. In a replica set one node is primary node and remaining
nodes are secondary.
3. All data replicates from primary to secondary node.
4. At the time of automatic failover or maintenance, election
establishes for primary and a new primary node is
elected.
5. After the recovery of failed node, it again join the replica
set and works as a secondary node.

MongoDB

How replication works in MongoDB

MongoDB

Set up a replica set


convert standalone mongod instance to a replica set
> mongod --port 27017 --dbpath "D:\set up\mongodb\data"
--replSet rs0
Add members to replica set
Suppose your mongod instance name is mongod1.net and
it is running on port 27017. To add this instance to
replica set issue the command rs.add() in mongo client.
>rs.add("mongod1.net:27017")

MongoDB

MongoDB Sharding
Sharding is the process of storing data records across
multiple machines and it is MongoDB's approach to
meetingthe demands of data growth.
As the size of the data increases, a single machine may
not be sufficient to store the data nor provide an
acceptable read and write throughput
Sharding solves the problem with horizontal scaling. With
sharding, you add more machines to support data growth
and the demands of read and write operations

MongoDB

Sharding

MongoDB

shrding
Shards: Shards are used to store data. They provide high
availability and data consistency. In production environment
each shard is a separate replica set.
Config Servers: Config servers store the cluster's metadata.
This data contains a mapping of the cluster's data set to the
shards.
Query Routers: Query Routers are basically mongos
instances, interface with client applications and direct
operations to the appropriate shard

MongoDB

Back up & Restore


Mongodump
back all data of the server to directory /bin/dump/
mongodump --collection COLLECTION --db DB_NAME
mongodump collection mycol --db test
mongorestore

Вам также может понравиться