Академический Документы
Профессиональный Документы
Культура Документы
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
MongoDB
MongoDB
MongoDB
MongoDB
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
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
MongoDB
MongoDB
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
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
MongoDB
MongoDB
MongoDB
MongoDB
db.users.remove({"favorites.cities": "Cheyenne"})
> db.users.drop()
MongoDB
Create collection
db.createCollection("mycollection")
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.
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
MongoDB
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})
MongoDB
MongoDB
MongoDB
db.numbers.createIndex({num: 1})
db.numbers.getIndexes()
db.numbers.find({num: {"$gt":
19995 }}).explain("executionStats")
db.post.find({'comments.user':/^us/})
MongoDB
MongoDB
MongoDB
MongoDB
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
MongoDB
MongoDB
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
MongoDB
MongoDB
MongoDB
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