Introduction to MongoDB
The Big Picture
MongoDB != RDBMS
Setting up the server
Manipulating data
Indexing
Miscellaneous
MongoDB != RDBMS
Impedance Mismatch
Scalability - MongoDB
No schema
Single document write scope
Eventual consistency
Capped Collection
Consistency 1 Server
Consistency Multi Server
Consistency Choices - Complete
Consistent and durable!
Consistency Choices - Fire & Forget
Consistency Choices - Majority
Inconsistency
Scalability - MongoDB
No schema
Click streams
Logging
Batch imports
Vendor integration
Summary
MongoDB
Fast
Scalable
Tunable consistency
No tables
No schema
Introduction to MongoDB
Installation and Startup
Getting the software
installation
Running the server
mongodb.org
MongoDB is written in c++
MongoDB runs on port 27017
start MongoDB
command options
-v //verbose
--logpath //logfile
--port //port number
--maxConns //max number of simultaneous connections,20000 by default
--dbpath //database dir
-f //config file
get help
help
Replica Set
Minimal Replica Set in MongoDB
One Primary DB,One Secondary DB and One Arbiter DB,those three DBs can run on one same physical server
When the primary DB is down,one of the Secondary DB will take over the primary DB's
Secondary DB does not accept write command
Arbiter DB
does not have any data
Replica Set Demo
Three data dir
/mongodb/db1
/mongodb/db2
/mongodb/db3
@REM Primary
start 'a' mongod --dbpath /mongodb/db1 --port 30000 --replSet "demo"
@REM Secondary
start 'b' mongod --dbpath /mongodb/db2 --port 40000 --replSet "demo"
@REM Arbiter
start 'c' mongod --dbpath /mongodb/db3 --port 50000 --replSet "demo"
connect one of the db
mongo --port 3000
db.getMongo()
define the Replica set
var demoConfig = {
"_id":'demo',
"members":[
{
"_id":0,
"host":"localhost:30000",
"priority":10
},
{
"_id":1,
"host":"localhost:40000"
},
{
"_id":2,
"host":"localhost:50000",
"arbiterOnly":true
}
]
};
rs.initiate(demoConfig)
The mongo shell prompt will change:
demo:PRIMARY>
Verify Replication Works
db.foo.save({_id:1,value:"Hello World"})
db.foo.find()
connect to the Secondary DB
mongo --port 40000
demo:SECONDARY>
db.foo.find()
db.setSlaveOk()
//slaveOk is also a connection level setting
db.foo.find()
Replic Set Failover
if the Primary DB is down,the Secondary DB will take over
if the Primary DB is up again, will take become a Secondary DB for a moment then the primary and the Secondary DB will exchange
rs.status() //replica status
2.The MongoDB shell
Shell Role in Ecosystem
Shell Modes
Interactive Shell
Runs Javascript
Scripting - not just issuing CRUD
Useful for
Administrative tasks
Spelunk(洞穴探险) Data
Fix or modify some documents
"Blind" Shell
runs JavaScript
Run Administrative scripts
Run single ad-hoc command
Useful for
Administrative tasks
Batch processing
Blind Command
//single command: --eval
mongo server1/admin --eval 'db.runCommand({logRotate:1})'
Blind script
//run a script: script name
mongo server1 myDailyChores.js
Script -> Interactive
//run script,then get in shell: --shell
mongo server1 myDailyChores.js --shell
Using eval
mongo localhost/admin --eval "db.runCommand({logRotate:1})"
return [object Object]
mongo localhost/admin --eval "printjson(db.runCommand({logRotate:1}))"
return
{ "ok" : 1 }
Substantial Scripts
userCount.js
var userCount = function(){
var count = db.users.count();
var entry = {_id:Date(),n:count};
db.UserCountHistory.save(entry);
print("\nToday's User Count:" + entry.n);
};
userCount();
mongo userCount.js
Execute Script Before Enter
safer.js
DB.prototype.dropDatabase = function() {
print("Don't do it man!");
};
db.dropDatabase = DB.prototype.dropDatabase;
mongo safer.js --shell
Shell Keys and Shortcuts
Quick ref - keystrokes
Keys Function
<- -> Move cursor right/left
Ctrl + <- Ctrl + -> Move cursor right/left
up/down Scroll history
Home or Ctrl + A Start of line
End or Ctrl + E End of line
Ctrl + k Delete to end of line
Ctrl + L Clear screen
MultiLine Editing
External Editor Integration
External Editor
Complex scripts can use a 'big-boy' editor
Write up the shell to use your favorite editor
1//From the console:
set EDITOR="mySuperEditor.exe"//on windows
export EDITOR="vim"//on linux
2//From the shell:edit
mongo> myFunction = function (x){}
mongo> edit myFunction
Load script from Within
mongo>pwd()
mongo>load("safer.js")
User RC File
~/mongorc.js
mongo --norc //will not the mongorc.js
Common safety Usage Tip
Prevent Disaster
//sample .mongorc.js script
var _no_ = function(){print("Nope!");}
DB.prototype.dropDatabase = _no_;
db.dropDatabase = db.prototype.dropDatabase;
DB.prototype.shutdownServer = _no_;
db.shutdownServer = DB.prototype.shutdownServer;
Shell Alternatives
Command line benefits
Free
Comes with
Interactive
Scriptable
Makes you proficient
3.Storing Data
Overview
Storage Engine
Saving documents
Updating documents
Storage Internals
How data stored?
Your Application
|
Server Memory
|
Disk
Memory Mapped File
How is data formatted? - BSON
besonspec.org
Save Data
#1:A document must have an _id field.
#2:There are very few rules.
one document can store maximum 16MB files
Collections
show collections
db.foo.save({_id:1,x:10})
Document Id
The _id filed can be:
db.foo.save({_id:1});
db.foo.save({_id:3.14});
db.foo.save({_id:"Hello"});
db.foo.save({_id:ISODate()});
db.foo.save({_id:{a:'x',b:2}});
array is an exception
db.users.save({name:"Bob"})
ObjectId
ObjectId()
ObjectId().getTimestamp()
Insert
Insert with Id
Complex Document
Save Danger
update Command
Atomic within a document
db.foo.update(query,update,options)
Update Demo
set Operator
used to add field to document
unset Operator
Used to remove field from document
rename Operator
push Operator
addtoSet Operator
add element to array if not exists
Pull Operator
Pop Operator
Array Type
Multi Update
{multi:true}
Find and Modify - only modify 1 document
db.foo.findAndModify({
query:<document>,//Which Document?
update:<document>,//what Change?
upsert:<boolean>,//Insert if not exists?
remove:<boolean>,//Delete it?
new:<boolean>,//Return new/old?
sort:<document>,//Query order
fields:<document>//Return what?
});
Query with sort
Demo Find and Modify
Documentation
www.mongodb.org
mongo docs
5.Finding Documents
Overview
Query criteria
Field selection
Cursor operation
Fiend
db.foo.find(query,projection)
query -> Which documents
projection -> Which fields
Equality
Projections
Comparison
$gt
$lt
$lte
$gte
$gt $lt
$gte $lte
$not
$in
$nin
Arrays
$all
$min
dot notation
sub document
null and $exists
And -> ,
More projection
cont not mix including and excluding
Cursor
var c = db.animals.find({},{name:1})
c.size()
c.hasNext()
c.foreach(function(d){print(d.name)})
Sort
sort({info.type:1,name:-1})
limit
skip
findOne
6.Indexing
Overview
Mongo indexes
Indexing a collection
Usage by queries
Scan 1s Bad
Index Theory
Sort Uses Index
Index Types
Indexes in Mongo
Regular(B-tree)
Geo
Text
Hashed(used on)
TTL(time to live)
Create Index
db.foo.ensureIndex(keys,options)
keys -> Which fields?
In what Order?
Geo/Text
options -> Name?
Build now?
Unique
Sparse?
TTL
language?
System indexes collection
db.system.indexes.find()
ns = namespace
explain
db.animals.find().explain();
add index
db.animals.ensureIndex({name:1})
1 means build the index asc
-1 means build the index desc
nsscanned
MUlti-Term Query
Comparison
IndexBounds
Scanned && Returned
dropIndex()
dropIndex("index_name");
Nested fields
db.animals.ensureIndex({"info.color":1})
Similar to dot-notation,don't forget quotation marks
Array fields
multikey index
db.animals.ensureIndex({tags:1})
sort
scanAndOrder
Unique
db.animals.ensureIndex({name:1},{unique:true})
Sparse
db.animals.ensureIndex({name:1},{sparse:true})
Compound
db.animals.ensureIndex({name:1,tags:1},{sparse:true})
Index:{tags,name}
Query:{name} - no tags terms!
Sort Direction
Covering index
indexOnly
Dead Weight
Background Build
db.animals.ensureIndex({name:1},{background;true})
may be much longer then forground
index file size may be much larger then forground
Index name
one index can mave max 31 fields in it
index name <= 128 bytes
db.animals.ensureIndex({name:1},{name;'indexname'})