MongoDB Cheat Sheet

A Programming › MongoDB cheat sheet in English by ovi_mihai

MongoDB

91,007 views 20,494 downloads 27 Mar 2013 History
4.5 avg · 9 ratings

Basic commands

mongo
Start Mongo
show dbs
Show databases
use mydb
User database named "­myd­b"
db
Show selected database
help
Get help
show collec­tions
Show collec­tions from a database

Finds - db.col­lection

.find()
Displays documents from "­col­lec­tio­n" (first 10)
it
Type "­it" to see more documents after the prev command
.find(­que­ry[­,fi­elds])
Find all documents by condit­ions, with optional fields selection
.findO­ne(­query)
Find one document by conditions
.find(­que­ry).li­mit­(n:­number)
Find n document by conditions
.find(­).p­retty()
Format results in Mongo Shell
.find(­).s­ort­(ke­y:1|-1)
Sort by key in ascending (1) or descending (-1) order
.find(­).s­kip(5)
Skip 5 documents (similar to offset)

Modify - db.col­lection

.inser­t(d­ocu­ment(s) [,opti­ons])
Insert a new document or multiple docume­nts(if provided an array of documents) in the collec­tion. Options: writeC­oncern, ordered
.inser­tOn­e(d­ocu­ment, [,opti­ons])
Same as insert from 3.2
.inser­tMa­ny(­array of documents, [,opti­ons])
Same as in sert from 3.2
.updat­e(q­uery, update [,opti­ons])
Update the documents matched by the query. See update operators. Options: upsert­(insert if no match), multi(aply to multiple elements), writeC­oncern
.remov­e(query [,opti­ons])
Remote some documents from a collec­tion. {} for all. options: {justOne, writeC­oncern}
.delet­eOn­e(q­uery), delete­Man­y(q­uery)
Similar to remove

Queries

_id
Search by ID
key: value
Search through key-value combin­ation
{ subkey: value }
Search with subdoc­ument
$in : [ e1, e2, .. ]
Search IN Array
queries are repres­ented through JSON objects

Query Selectors : Element

$exist­s:B­oolean
Check if property exists or not
$mod:[D,R]
Checks if a property divided by D has the specified R
$type:Int
Checks if property is the specified type

Query Selectors : Comparison

$gt:Val
Greater then Val
$gte:Val
Greater then equals Val
$lt:Val
Lower then Val
$lte:Val
Lower then equals Val
$all:Array
All Array elements are included in field array value
$in:Array
Elements with values contained in Array
$nin:Array
Elements with values Not contained in Array
$ne:Val
Not equal
Val can be any Scalar Integer, String, Date, etc

Query Selectors : Logical

$and:AOE
AND operation between all AOE expres­sions
$nor:AOE
all AOE expres­sions must fail
$not:Expr
Negate a SubDoc­ument (doesn­'t work with $regex)
$or:AOE
OR operation between all AOE expres­sions
Expr = Expression
AOE = Array Of Expres­sions
( eg: Expression = price : 20 )

Badges

Useful Cheat Sheet Great Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Appreciated Cheat Sheet Downloaded from Unusual Locations × 8

Comments (2)

ovi_mihai 18:57 23 Jan 2016

I'm on it, but I have so little time this days :(

Reply
Alexander 10:13 11 Jun 2015

Hello, could you update this cheatsheet with insert, update and remove operations?

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.