MongoDB Cheat Sheet
by Ovidiu Anicai (ovi_mihai) via cheatography.com/4776/cs/916/
Basic commands Modify - db.collection (cont)
mongo Start Mongo .deleteOne(query), deleteMany(query) Similar to remove
show dbs Show databases
Queries
use mydb User database named "mydb"
db Show selected database _id Search by ID
help Get help key: value Search through key-value combination
show collections Show collections from a database { subkey: value } Search with subdocument
$in : [ e1, e2, .. ] Search IN Array
Finds - db.collection
queries are represented through JSON objects
.find() Displays documents from "collection" (first
10) Query Selectors : Element
it Type "it" to see more documents after the $exists:Boolean 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 conditions, with
$type:Int Checks if property is the specified type
optional fields selection
.findOne(query) Find one document by conditions D = Divisor
R = Reminder
.find(query).limit(n:number) Find n document by conditions
Bson Types
.find().pretty() Format results in Mongo Shell http://docs.mongodb.org/manual/reference/glossary/#term-bson
.find().sort(key:1|-1) Sort by key in ascending (1) or descending
(-1) order Query Selectors : Comparison
.find().skip(5) Skip 5 documents (similar to offset) $gt:Val Greater then Val
$gte:Val Greater then equals Val
Modify - db.collection
$lt:Val Lower then Val
.insert(document(s) [,options]) Insert a new document or multiple
$lte :Val Lower then equals Val
documents(if provided an array of
documents) in the collection. Options: $all:Array All Array elements are included in field array value
writeConcern, ordered
$in:Array Elements with values contained in Array
.insertOne(document, Same as insert from 3.2
$nin:Array Elements with values Not contained in Array
[,options])
$ne:Val Not equal
.insertMany(array of Same as in sert from 3.2
documents, [,options]) Val can be any Scalar Integer, String, Date, etc
.update(query, update Update the documents matched by the
[,options]) query. See update operators. Options: Query Selectors : Logical
upsert(insert if no match), multi(aply to
$and:AOE AND operation between all AOE expressions
multiple elements), writeConcern
$nor:AOE all AOE expressions must fail
.remove(query [,options]) Remote some documents from a
collection. {} for all. options: {justOne, $not:Expr Negate a SubDocument (doesn't work with $regex)
writeConcern} $or:AOE OR operation between all AOE expressions
Expr = Expression
AOE = Array Of Expressions
( eg: Expression = price : 20 )
By Ovidiu Anicai (ovi_mihai) Published 27th March, 2013. Sponsored by Readability-Score.com
cheatography.com/ovi-mihai/ Last updated 10th May, 2016. Measure your website readability!
0v1.ro Page 1 of 1. https://readability-score.com