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

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.
7/22/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

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

> 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

> 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

> 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

MongoDB

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