Mongodb
Mongodb
In Production
http://www.mongodb.org/about/production-deployments/
NoSQL
• Key-value
• Graph database
• Document-oriented
• Column family
3
Document store
RDBMS MongoDB
Database Database
Table, View Collection
Row Document (JSON, BSON)
Column Field
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard
4
Document store
RDBMS MongoDB
Database Database > db.user.findOne({age:39})
Table, View Collection {
"_id" : ObjectId("5114e0bd42…"),
Row Document (JSON, BSON)
"first" : "John",
Column Field "last" : "Doe",
Index Index "age" : 39,
Join Embedded Document"interests" : [
Foreign Key Reference "Reading",
"Mountain Biking ]
Partition Shard
"favorites": {
"color": "Blue",
"sport": "Soccer"}
} 5
MongoDB
• MongoDB is an open-source document database that provides
high performance, high availability, and automatic scaling.
A record in MongoDB is a document, which is a data structure
composed of field and value pairs As:
• {
name:”ABC”,
age:18,
gender:”Male”
}
Significantly, MongoDB supports neither joins nor
transactions.Instead of tables, a MongoDB database stores its data
in collections, A collection holds one or more documents, which
corresponds to a record or a row in a relational database table,
and each document has one or more fields, which corresponds to
a column in a relational database table. 6
MongoDB uses dynamic schemas. You can create collections
without defining the structure, i.e. the fields or the types of their
values, of the documents in the collection.
db.createCollection(“users”)
7
• 2) To Insert values into Table:
• MongoDB
=> db.users.insert({user_id:”abc33″,name:”sayali”,age:22})
• Equivalent SQL: Insert into values(“abc33”,”sayali”,22)
• 3) To Update Table Value:
• MongoDB => db.users.update( { },
{ $set: { join_date: new Date() } },
{ multi: true })
• 4) To Drop column from Table:
• MongoDB => db.users.update( { },
{ $unset: { join_date: new Date() } },
{ multi: true } ) 8
• 6) Sort operation:
• To sort field by Asending and Desending order
ASC:value=1
Desc:value=-1
• MongoDB => db.users.find({}).sort( { age: 1 } )
• Equivalent SQL: select * from users Order by age ASC;
• 7) To use count() function:
MongoDB => db.users.count()
• 8) To find out Distinct column Value:
• MongoDB => db.users.distinct( “age” )
• 9) To Delete Row:
9
• MongoDB => db.users.remove( { status: “D” } )
CRUD
• Create
• db.collection.insert( <document> )
• db.collection.save( <document> )
• db.collection.update( <query>, <update>, { upsert: true } )
• Read
• db.collection.find( <query>, <projection> )
• db.collection.findOne( <query>, <projection> )
• Update
• db.collection.update( <query>, <update>, <options> )
• Delete
• db.collection.remove( <query>, <justOne> )
10
CRUD example
> db.user.find ()
> db.user.insert({ {
first: "John", "_id" : ObjectId("51…"),
last : "Doe", "first" : "John",
age: 39 "last" : "Doe",
}) "age" : 39
}
> db.user.update(
{"_id" : ObjectId("51…")},
{ > db.user.remove({
$set: {
"first": /^J/
age: 40,
salary: 7000} }) 11
}
)
Features
• Document-Oriented storege
• Full Index Support
• Replication & High Agile
Availability
• Auto-Sharding
• Querying
• Fast In-Place Updates Scalable
• Map/Reduce
12
Replica Sets Host1:10000
• Geospatial features
13
Sharding
• Partition your data
• Scale write
throughput
• Increase capacity
• Auto-balancing
shard1 shard2
Host1:10000 Host2:10010
configdb
Host3:20000
14
Host4:30000 Client
Mixed
shard1
...
Host1:10000
shardn
Host2:10001 Host4:10010
Host3:10002
replica1
configdb
Host5:20000
Host6:30000 Client
Host7:30000 15
Other features
• Easy to install and use
• Detailed documentation
• Various APIs
• JavaScript, Python, Ruby, Perl, Java, Java, Scala, C#, C++, Haskell,
Erlang
• Community
• Open source
16
Theory of noSQL: CAP
• Many nodes
C
• Nodes contain replicas of
partitions of data
• Consistency
• all replicas contain the same
version of data
• Availability A P
• system remains operational on
failing nodes
• Partition tolarence CAP Theorem:
• multiple entry points satisfying all three at the
• system remains operational on same time is impossible 17
system split
Theory of noSQL: CAP
• Many nodes
C
• Nodes contain replicas of
partitions of data
• Consistency
• all replicas contain the same
version of data
• Availability A P
• system remains operational on
failing nodes
• Partition tolarence CAP Theorem:
• multiple entry points satisfying all three at the
• system remains operational on same time is impossible 18
system split
ACID - BASE
•Basically
•Atomicity
Available (CP)
•Consistency •Soft-state
•Isolation •Eventually
•Durability
consistent (AP)
19
For more details of MongoDB,
please visit the links below
• https:// • https://
www.mongodb.com/ hevodata.com/
docs/manual/ learn/mongodb-join-
tutorial/query- two-collections/
documents/
20