0% found this document useful (0 votes)
6 views3 pages

Hanysamir - Mongodb Cheat Sheet Issr Master

The MongoDB Cheat Sheet by Hany Samir provides a comprehensive overview of essential MongoDB commands, including syntax and examples for operations such as inserting, updating, deleting, and querying documents. It also covers aggregation pipelines, logical and comparison operators, and index management. This resource is designed for quick reference to facilitate efficient use of MongoDB functionalities.

Uploaded by

thabeswar2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Hanysamir - Mongodb Cheat Sheet Issr Master

The MongoDB Cheat Sheet by Hany Samir provides a comprehensive overview of essential MongoDB commands, including syntax and examples for operations such as inserting, updating, deleting, and querying documents. It also covers aggregation pipelines, logical and comparison operators, and index management. This resource is designed for quick reference to facilitate efficient use of MongoDB functionalities.

Uploaded by

thabeswar2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MongoDB Cheat Sheet "issr Master" Cheat Sheet

by Hany Samir (hanysamir) via cheatography.com/198431/cs/41986/

Basinc MongoDB commands Update

Commmand Descri​ption Example syntax Descri​ption Example


mongod start the mongod --dbpath /home/​mongo -- update​​On​e​(​‐ update one db.sal​es.u​pd​ate​One​({"i​tem​"​:"ab​c"}, { "​‐
mongo logpath /home/​mon​go/​mon​god.log -- fi​​lter, data, document $se​t" : {"pr​ice​": 22} } )
service bind_ip 0.0.0.0 options)
mongosh command mongosh update​Man​‐ update db.sal​es.u​pd​ateMany ( { "​"​_id​"​"​:{""$​‐
run y(f​i​lter, data, matching gte​"​":14, "​"​$lt​e"":16} } , { $unset : { "​"​‐
mongodb options) documents. pri​ce"" : "​" "​"} } )
shell
$set to update the field value
show dbs list show dbs $unset will remove the field
databases
use Switch to use company Index
db_name databa​se db syntax Descri​ption Example
db show current db create​‐ Creates db.pro​jec​t.c​rea​teI​ndex( { "​pnu​mbe​r": 1,
database Index( indexes on "​Pna​me": -1 } , { "​nam​e": "​pno​_na​me" ,
name {keys} , collec​tions. "​uni​que​": true} )
show List current show collec​tions {options} )
collec​tions database dropIn​dex​‐ Delete db.pro​jec​t.d​rop​Ind​ex(​"​pno​_na​me")
collec​tions (index) index
getInd​‐ List db.pro​jec​t.g​etI​nde​xes()
Insert
exes() indexes on
syntax Descri​ption Example collec​tion.
insertOne insert one db.peo​ple.in​ser​tOne({ "​ssn​":1, if index name was not specified withing options, the name will be
(data, document "​nam​e":"A​li",​"​age​"​:20}) generated automa​tic​ally.
options) 1 = ascending order
insert​Man​‐ insert many "​db.s​al​es.i​ns​ert​Many([ { "​"​ite​‐ -1 = descending order
y([​{do​cum​‐ documents, m"": "​"​abc​"​", "​"​pri​ce"": 10}, { "​"​‐
ent​1},​{do​‐ square brackets ite​m"": "​"​xyz​"​", "​"​pri​ce"": 5 } ])" Find
cum​ent2}]) are mandatory
syntax Descri​ption Example
findOn​​e(​f​i​‐ find first db.emp​loy​ee.f​in​dOn​e({​"​dno​": 8},
Delete
lter, matching {"_i​d":0​,"fn​ame​"​:1,​"​lna​me":1})
syntax Descri​ption Example options) document
delete​One​​‐ delete the first matching db.pro​jec​t.d​ele​teOne( { find(f​​ilter, find all db.emp​loy​ee.f​in​d({​"​dno​": 8},{"_i​‐
(f​i​lter, document from collection "​pnu​mbe​r": 1 } ) options) matching d":0​,"fn​ame​"​:1,​"​lna​me":1})
options) documents
delete​Man​‐ delete all matching db.pro​jec​t.d​ele​teMany(
y(f​i​lter, documents from the project { "​plo​cat​ion​": "​Sta​ffo​rd"
options) collection })

By Hany Samir (hanysamir) Published 8th January, 2024. Sponsored by Readable.com


cheatography.com/hanysamir/ Last updated 8th January, 2024. Measure your website readability!
Page 1 of 3. https://readable.com
MongoDB Cheat Sheet "issr Master" Cheat Sheet
by Hany Samir (hanysamir) via cheatography.com/198431/cs/41986/

Query methods Aggreg​ation Pipeline (cont)

syntax Descri​ption Example $lookup Performs a left outer join to a collection in db.ord​‐
sort({​fie​ld_​‐ find first matching document db.sal​es.f​in​d().so​‐ the same database to filter in documents ers.ag​gre​‐
name:1 or -1}) rt(​{"pr​ice​": 1}) from the "​joi​ned​" collection for proces​sing. gate( [ {
The $lookup stage adds a new array field $lookup: {
limit(n) Limits the number of db.sal​es.f​in​d().li​‐
to each input document. The new array from: "​inv​‐
documents mit(3)
field contains the matching documents ent​ory​",
skip(n) Skips over the specified db.sal​es.f​in​d().sk​‐
from the "​joi​ned​" collec​tion. The $lookup localF​ield: "​‐
number of documents ip(3)
stage passes these reshaped documents ite​m", foreig​‐
1 = ascending order to the next stage. nField: "​‐
-1 = descending order sku​", as: "​‐
inv​ent​ory​‐
Aggreg​ation Pipeline _do​cs" } } ] )

syntax Descri​ption Example The $lookup takes a document with these fields:

$group The $group stage separates db.emp​loy​ee.a​gg​reg​‐ from Specifies


documents into groups according ate([ { "​$gr​oup​" : { "​‐ the
to a "​group key". The output is _id​" : null, "​ave​rag​‐ collection in
one document for each unique eSa​lar​y": { "​$av​g": "​‐ the same
group key. $sa​lar​y" } } } ]) database to
perform the
Accumu​lator Operator
join with.
$avg Returns an average of
localField Specifies
numerical values.
the field
$max Returns the highest
from the
expression value for
documents
each group.
input to the
$min Returns the lowest $lookup
expression value for stage.
each group.
foreig​nField Specifies
$sum Returns a sum of the field
numerical values. from the
Ignores non-nu​meric documents
values. in the from
collec​tion.
as Specifies
the name of
the new
array field to
add to the
input
documents.

By Hany Samir (hanysamir) Published 8th January, 2024. Sponsored by Readable.com


cheatography.com/hanysamir/ Last updated 8th January, 2024. Measure your website readability!
Page 2 of 3. https://readable.com
MongoDB Cheat Sheet "issr Master" Cheat Sheet
by Hany Samir (hanysamir) via cheatography.com/198431/cs/41986/

Logical operators

$and:[​‐ AND operation between all array of expres​​sions


array]
$or:[a​‐ OR operation between all array of expres​​sions
rray]
$nor:[​‐ All array expres​​sions must fail.
array]
$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.

By Hany Samir (hanysamir) Published 8th January, 2024. Sponsored by Readable.com


cheatography.com/hanysamir/ Last updated 8th January, 2024. Measure your website readability!
Page 3 of 3. https://readable.com

You might also like