An introduction to MongoDB
In Production
[Link]
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 > [Link]({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
[Link] 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.
[Link](“users”)
Following are some DDL+DML Statements in MongoDB:
•1) To Create Table:
•MongoDB => [Link](“users”)
•Equivalent SQL:create table users(user_id varchar2(10),name
varchar2(10),age number)
7
• 2) To Insert values into Table:
• MongoDB
=> [Link]({user_id:”abc33″,name:”sayali”,age:22})
• Equivalent SQL: Insert into values(“abc33”,”sayali”,22)
• 3) To Update Table Value:
• MongoDB => [Link]( { },
{ $set: { join_date: new Date() } },
{ multi: true })
• 4) To Drop column from Table:
• MongoDB => [Link]( { },
{ $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 => [Link]({}).sort( { age: 1 } )
• Equivalent SQL: select * from users Order by age ASC;
• 7) To use count() function:
MongoDB => [Link]()
• 8) To find out Distinct column Value:
• MongoDB => [Link]( “age” )
• 9) To Delete Row:
9
• MongoDB => [Link]( { status: “D” } )
CRUD
• Create
• [Link]( <document> )
• [Link]( <document> )
• [Link]( <query>, <update>, { upsert: true } )
• Read
• [Link]( <query>, <projection> )
• [Link]( <query>, <projection> )
• Update
• [Link]( <query>, <update>, <options> )
• Delete
• [Link]( <query>, <justOne> )
10
CRUD example
> [Link] ()
> [Link]({ {
first: "John", "_id" : ObjectId("51…"),
last : "Doe", "first" : "John",
age: 39 "last" : "Doe",
}) "age" : 39
}
> [Link](
{"_id" : ObjectId("51…")},
{ > [Link]({
$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
• Redundancy and Failover Host2:10001
• Zero downtime for Host3:10002
upgrades and maintaince replica1
• Master-slave replication Client
• Strong Consistency
• Delayed Consistency
• 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://
[Link]/ [Link]/
docs/manual/ learn/mongodb-join-
tutorial/query- two-collections/
documents/
20