mongodb
mongodb
640K ought to be
enough for anybody.
Why mongoDB?
• https://www.youtube.com/watch?v=CvIr-2lMLsk
2013
In Production
http://www.mongodb.org/about/production-deployments/
NoSQL
• Key-value
• Graph database
• Document-oriented
• Column family
6
BASE Properties
• Basic Availability
– The database appears to work most of the time.
• Soft-state
– Stores don’t have to be write-consistent, nor do
different replicas have to be mutually consistent all
the time.
• Eventual consistency
– Stores exhibit consistency at some later point (e.g.,
lazily at read time).
What Is mongoDB?
A document-oriented database
documents encapsulate and encode data (or
information) in some standard formats or encodings
NoSQL database
uses BSON format
schema-less
No more configuring database columns with types
No transactions
No joins
9
SQL Vs MongoDB
SQL Concepts MongoDB Concepts
database database
table Collection
Row Document 0r BSON Document
Column Field
Index Index
Table Join Embedded documents & Linking
sqlplus mongo
Database Client mysql
• MongoDB Vs Relational DBMS
• Collection vs table
• Document vs row
• Field vs column
• schema-less vs schema-oriented
13
The Basics
• A MongoDB instance may have zero or more databases
2013
• MongoDB Indexes is much like their RDBMS counterparts.
Table Creation in Oracle
Output: Teacher_Info
17
Insert Command:
{ "_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
"Last Name": "PELLERIN",
"First Name": "Franck",
"Date of Birth": "09-19-1983",
"Address": "1 chemin des Loges",
"City": "VERSAILLES" }
19
Document store
RDBMS MongoDB
Database Database
Table, View Collection
Column Field
Index Index
Join Embedded Document
Partition Shard 20
Document store
RDBMS MongoDB
> db.user.findOne({age:39})
Database Database {
Table, View Collection "_id" : ObjectId("5114e0bd42…"),
"first" : "John",
Row "last" : "Doe",
Document (JSON, BSON)
"age" : 39,
Column Field "interests" : [
Index Index "Reading",
Join Array
Embedded Document
"Mountain Biking ]
Foreign Key Reference "favorites": {
"color": "Blue",
Partition Shard "sport": "Soccer"}
} 21
Embedded document
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 22
db.collection.remove( <query>, <justOne> )
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: {
age: 40,
"first": /^J/
salary: 7000} }) 23
}
)
The components of a MongoDB find operation.
MongoDb:
At the document level, update ()operations can add fields to
existing documents using the $set operator.
Ex:
db.Teacher_info.update( { }, { $set: { join_date: new Date() } },
{ multi: true} )
2. Drop Command:
Oracle:
DROP TABLE Teacher_info
Mongo:
db.Teacher_info.drop()
3. INSERT A DOCUMENT WITH INSERT()
METHOD
db.inventory.find( { } )
OR
db.inventory.find()
Conditional Operators
SPECIFY EQUALITY CONDITION
The following example retrieves from the inventory
collection all documents where the type field has
the value snacks:
Specify OR Conditions
In the following example, the compound query
document selects all documents in the collection
where the value of the type field is 'food' and
either the qty has a value greater than ($gt) 100
or the value of the price field is less than ($lt)
9.95:
ARRAYS
{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8,
9 ]}
{ _id: 6, type: "food", item: "bbb", ratings: [ 5,
9 ]}
db.inventory.find( { ratings: [ 5, 8, 9 ] } )
Then we can find all documents with both "apple" and "banana"
elements by querying with "$all":
> db.food.find({fruit : {$all : ["apple", "banana"]}})
Output:-
{"_id" : 1, "fruit" : ["apple", "banana", "peach"]}
{"_id" : 3, "fruit" : ["cherry", "banana", "apple"]}
$size
A useful conditional for querying arrays
is "$size", which allows you to query for
arrays of a given size. Here’s an
example:
> db.food.find({"fruit" : {"$size" : 3}})
$slice
The special "$slice" operator
can be used to return a subset of
elements for an array key.
For example, suppose we had a
blog post document and we
wanted to return the first 10
comments:
> db.blog.posts.findOne(criteria,
{"comments" : {"$slice" : 10}})
Alternatively, if we wanted the last 10
comments, we could use -10:
db.blog.posts.findOne(criteria,
{"comments" : {"$slice" : -10}})