MongoDB Cheat Sheet
by
Ovidiu Anicai (ovi_mihai)
via
Basic commands
Modify - db.col l ection (cont)
mongo
Start Mongo
.delet e On e (q u ery), delete M an y (q u ery)
Similar to remove
show dbs
Show databases
Queries
use mydb
User database named " m yd b "
_id
Search by ID
db
Show selected database
key: value
Search through key-value combin a tion
help
Get help
{ subkey: value }
Search with subdoc u ment
show collec t ions
Show collec t ions from a database
$in : [ e1, e2, .. ]
Search IN Array
Finds - db.col l ection
queries are repres e nted through JSON objects
.find()
Displays documents from " c ol l ec t io n " (first
10)
Query Selectors : Element
it
Type " i t" to see more documents after the
$exist s :B o olean
Check if property exists or not
prev command
$mod:[D,R]
Checks if a property divided by D has the specified R
.find(query[,fields])
Find all documents by condit i ons, with
$type:Int
Checks if property is the specified type
optional fields selection
D = Divisor
.findO n e( q uery)
Find one document by conditions
R = Reminder
.find(query).limit(n:number)
Find n document by conditions
Bson Types
.find( ) .p r etty()
Format results in Mongo Shell
http:/ / do c s.m o n g od b .o r g/ m an u al / re f er e nc e /g l os s ar y /# t er m -bson
.find().sort(key:1|-1)
Sort by key in ascending (1) or descending
Query Selectors : Comparison
(-1) order
.find( ) .s k ip(5)
Skip 5 documents (similar to offset)
$gt : Val
Greater then Val
$gte : Val
Greater then equals Val
Modify - db.col l ection
$lt : Val
Lower then Val
.insert(document(s) [,options])
Insert a new document or multiple
$lte : Val
Lower then equals Val
docume n ts(if provided an array of
documents) in the collec t ion. Options:
$all : A rray
All Array elements are included in field array value
writeC o ncern, ordered
$in : Array
Elements with values contained in Array
.inser t On e (d o cu m ent,
Same as insert from 3.2
$nin : A rray
Elements with values Not contained in Array
[,opti o ns])
$ne : Val
Not equal
.inser t Ma n y( a rray of
Same as in sert from 3.2
documents, [,opti o ns])
Val can be any Scalar Integer, String, Date, etc
.updat e (q u ery, update
Update the documents matched by the
Query Selectors : Logical
[,opti o ns])
query. See update operators. Options:
upsert ( insert if no match), multi(aply to
$and:AOE
AND operation between all AOE expres s ions
multiple elements), writeC o ncern
$nor:AOE
all AOE expres s ions must fail
.remov e (query [,opti o ns])
Remote some documents from a
$not:Expr
Negate a SubDoc u ment (doesn't work with $regex)
collec t ion. {} for all. options: {justOne,
writeC o ncern}
$or:AOE
OR operation between all AOE expres s ions
Expr = Expression
AOE = Array Of Expres s ions
( eg: Expression = price : 20 )
By Ovidiu Anicai (ovi_mihai)
Published 27th March, 2013.
Sponsored by
Last updated 10th May, 2016.
Measure your website readability!
Page 1 of 1.
0v1.ro