Cheatography
https://cheatography.com
Basic commands
mongo |
Start Mongo |
show dbs |
Show databases |
use mydb |
User database named "mydb" |
db |
Show selected database |
help |
Get help |
show collections |
Show collections from a database |
Finds - db.collection
.find() |
Displays documents from "collection" (first 10) |
it |
Type "it" to see more documents after the prev command |
.find(query[,fields]) |
Find all documents by conditions, with optional fields selection |
.findOne(query) |
Find one document by conditions |
.find(query).limit(n:number) |
Find n document by conditions |
.find().pretty() |
Format results in Mongo Shell |
.find().sort(key:1|-1) |
Sort by key in ascending (1) or descending (-1) order |
.find().skip(5) |
Skip 5 documents (similar to offset) |
Modify - db.collection
.insert(document(s) [,options]) |
Insert a new document or multiple documents(if provided an array of documents) in the collection. Options: writeConcern, ordered |
.insertOne(document, [,options]) |
Same as insert from 3.2 |
.insertMany(array of documents, [,options]) |
Same as in sert from 3.2 |
.update(query, update [,options]) |
Update the documents matched by the query. See update operators. Options: upsert(insert if no match), multi(aply to multiple elements), writeConcern |
.remove(query [,options]) |
Remote some documents from a collection. {} for all. options: {justOne, writeConcern} |
.deleteOne(query), deleteMany(query) |
Similar to remove |
|
|
Queries
_id |
Search by ID |
key: value |
Search through key-value combination |
{ subkey: value } |
Search with subdocument |
$in : [ e1, e2, .. ] |
Search IN Array |
queries are represented through JSON objects
Query Selectors : Element
$exists:Boolean |
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 expressions |
$nor:AOE |
all AOE expressions must fail |
$not:Expr |
Negate a SubDocument (doesn't work with $regex) |
$or:AOE |
OR operation between all AOE expressions |
Expr = Expression
AOE = Array Of Expressions
( eg: Expression = price : 20 )
|
Created By
0v1.ro
Metadata
Favourited By
and 9 more ...
Comments
Alexander 10:13 11 Jun 15
Hello, could you update this cheatsheet with insert, update and remove operations?
ovi_mihai, 18:57 23 Jan 16
I'm on it, but I have so little time this days :(
Add a Comment
Related Cheat Sheets
More Cheat Sheets by ovi_mihai