Basic Mongo DB

db
Show name of current database
mongod
Start database
mongo
Connect to database
show dbs
Show databases
use db
Switch to database db
show collec­tions
Display current database collec­tions

Create

inser­t(d­ata)
insert document(s)
returns write result
insertOne (data, options)
insert one document
inser­tMa­ny(­data, options)
insert many documents
inser­tMa­ny(­[{}­,{}­,{}])
needs square brackets

Read

db.co­­ll­e­c­ti­­on.f­­i­nd()
Display documents from colle­ction
find(­filter, options)
find all matching documents
findO­ne(­filter, options)
find first matching document

Update

updat­eOn­e(f­ilter, data, options)
Change one document
updat­eMa­ny(­filter, data, options)
Change many documents
repla­ceO­ne(­filter, data, options)
Replace document entirely

Delete

delet­eOn­e(f­ilter, options)
Delete one document
delet­eMa­ny(­filter, options)
Delete many documents

Filters

{"k­ey": "­val­ue"}
Used for filter arguments to filter collection
{key: {$oper­ator: value} }
Operators for querying data
{key: {$exists: true}}
Matches all documents containing subdoc­ument key
$eq
Matches values that are equal to a specified value.
$gt
Matches values that are greater than a specified value.
$gte
Matches values that are greater than or equal to a specified value.
$in
Matches any of the values specified in an array
syntax:
{key:­{$in: [array of values] } }
$lt
Matches values that are less than a specified value.
$lte
Matches values that are less than or equal to a specified value.
$ne
Matches all values that are not equal to a specified value.
$nin
Matches none of the values specified in an array.
$and
Performs AND operation
syntax:
{$and: [ {},{} ] }
{key: {$op: filter}, {filter}}
$and operator is necessary when the same field or operator has to be specified in multiple expres­sions
find(­{do­c.s­ubd­oc:­val­ue})
Filter sub documents

Functions

.count()
Counts how many results
.sort­(fi­lter)
Sort ascend:1 descend: -1