Show Menu
Cheatography

MongoDB Cheat Sheet "issr Master" Cheat Sheet by

MongoDB cheat sheet customized for Cairo University, Faculty of Graduate Studies for Statistical Research "Master"

Basinc MongoDB commands

Commmand
Descri­ption
Example
mongod
start the mongo service
mongod --dbpath /home/­mongo --logpath /home/­mon­go/­mon­god.log --bind_ip 0.0.0.0
mongosh
command run mongodb shell
mongosh
show dbs
list databases
show dbs
use db_name
Switch to databa­se db
use company
db
show current database name
db
show collec­tions
List current database collec­tions
show collec­tions

Insert

syntax
Descri­ption
Example
insertOne (data, options)
insert one document
db.peo­ple.in­ser­tOne({ "­ssn­":1, "­nam­e":"A­li",­"­age­"­:20})
insert­Man­y([­{do­cum­ent­1},­{do­cum­ent2}])
insert many documents, square brackets are mandatory
"­db.s­al­es.i­ns­ert­Many([ { "­"­ite­m"": "­"­abc­"­", "­"­pri­ce"": 10}, { "­"­ite­m"": "­"­xyz­"­", "­"­pri­ce"": 5 } ])"

Delete

syntax
Descri­ption
Example
delete­One­­(f­i­lter, options)
delete the first matching document from collection
db.pro­jec­t.d­ele­teOne( { "­pnu­mbe­r": 1 } )
delete­Man­y(f­i­lter, options)
delete all matching documents from the project collection
db.pro­jec­t.d­ele­teMany( { "­plo­cat­ion­": "­Sta­ffo­rd" } )

Update

syntax
Descri­ption
Example
update­­On­e­(­fi­­lter, data, options)
update one document
db.sal­es.u­pd­ate­One­({"i­tem­"­:"ab­c"}, { "­$se­t" : {"pr­ice­": 22} } )
update­Man­y(f­i­lter, data, options)
update matching documents.
db.sal­es.u­pd­ateMany ( { "­"­_id­"­"­:{""$­gte­"­":14, "­"­$lt­e"":16} } , { $unset : { "­"­pri­ce"" : "­" "­"} } )
$set to update the field value
$unset will remove the field

Index

syntax
Descri­ption
Example
create­Index( {keys} , {options} )
Creates indexes on collec­tions.
db.pro­jec­t.c­rea­teI­ndex( { "­pnu­mbe­r": 1, "­Pna­me": -1 } , { "­nam­e": "­pno­_na­me" , "­uni­que­": true} )
dropIn­dex­(index)
Delete index
db.pro­jec­t.d­rop­Ind­ex(­"­pno­_na­me")
getInd­exes()
List indexes on collec­tion.
db.pro­jec­t.g­etI­nde­xes()
if index name was not specified withing options, the name will be generated automa­tic­ally.
1 = ascending order
-1 = descending order

Find

syntax
Descri­ption
Example
findOn­­e(­f­i­lter, options)
find first matching document
db.emp­loy­ee.f­in­dOn­e({­"­dno­": 8},{"_i­d":0­,"fn­ame­"­:1,­"­lna­me":1})
find(f­­ilter, options)
find all matching documents
db.emp­loy­ee.f­in­d({­"­dno­": 8},{"_i­d":0­,"fn­ame­"­:1,­"­lna­me":1})
 

Query methods

syntax
Descri­ption
Example
sort({­fie­ld_­name:1 or -1})
find first matching document
db.sal­es.f­in­d().so­rt(­{"pr­ice­": 1})
limit(n)
Limits the number of documents
db.sal­es.f­in­d().li­mit(3)
skip(n)
Skips over the specified number of documents
db.sal­es.f­in­d().sk­ip(3)
1 = ascending order
-1 = descending order

Aggreg­ation Pipeline

syntax
Descri­ption
Example
$group
The $group stage separates documents into groups according to a "­group key". The output is one document for each unique group key.
db.emp­loy­ee.a­gg­reg­ate([ { "­$gr­oup­" : { "­_id­" : null, "­ave­rag­eSa­lar­y": { "­$av­g": "­$sa­lar­y" } } } ])
Accumu­lator Operator
 
$avg
Returns an average of numerical values.
 
$max
Returns the highest expression value for each group.
 
$min
Returns the lowest expression value for each group.
 
$sum
Returns a sum of numerical values. Ignores non-nu­meric values.
$lookup
Performs a left outer join to a collection in the same database to filter in documents from the "­joi­ned­" collection for proces­sing. The $lookup stage adds a new array field to each input document. The new array field contains the matching documents from the "­joi­ned­" collec­tion. The $lookup stage passes these reshaped documents to the next stage.
db.ord­ers.ag­gre­gate( [ { $lookup: { from: "­inv­ent­ory­", localF­ield: "­ite­m", foreig­nField: "­sku­", as: "­inv­ent­ory­_do­cs" } } ] )
The $lookup takes a document with these fields:
 
from
Specifies the collection in the same database to perform the join with.
 
localField
Specifies the field from the documents input to the $lookup stage.
 
foreig­nField
Specifies the field from the documents in the from collec­tion.
 
as
Specifies the name of the new array field to add to the input documents.

Logical operators

$and:[­array]
AND operation between all array of expres­­sions
$or:[a­rray]
OR operation between all array of expres­­sions
$nor:[­array]
All array expres­­sions must fail.
$not:Expr
Performs a logical NOT operation on the specified <op­era­tor­-ex­pre­ssi­on>

Comparison operators

syntax
Descri­ption
$eq
Matches values that are equal to a specified value.
$ne
Matches all values that are not 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.
$lt
Matches values that are less than a specified value.
$lte
Matches values that are less than or equal to a specified value.
$in
Matches any of the values specified in an array
$nin
Matches none of the values specified in an array.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets