Mongodb Cheat Sheet


MongoDB Cheat Sheet
Ovidiu Anicai (ovi_mihai)
Basic commands
Modify - db.col​ l ection (cont)
Start Mongo
.delet​ e On​ e (q​ u ery), delete​ M an​ y (q​ u ery)
Similar to remove
show dbs
Show databases
use mydb
User database named "​ m yd​ b "
Search by ID
Show selected database
key: value
Search through key-value combin​ a tion
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
Displays documents from "​ c ol​ l ec​ t io​ n " (first
Query Selectors : Element
Type "​ i t" to see more documents after the
$exist​ s :B​ o olean
Check if property exists or not
prev command
Checks if a property divided by D has the specified R
Find all documents by condit​ i ons, with
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 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
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 operation between all AOE expres​ s ions
multiple elements), writeC​ o ncern
all AOE expres​ s ions must fail
.remov​ e (query [,opti​ o ns])
Remote some documents from a
Negate a SubDoc​ u ment (doesn't work with $regex)
collec​ t ion. {} for all. options: {justOne,
writeC​ o ncern}
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.


00 votes

Related Articles

Related forms

Related Categories

Parent category: Education