mongosh MongoDB shell
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
mongosh "<uri>"
show dbsuse <db>show collections
db.<coll>.find()
db.<coll>.insertOne({})
db.<coll>.updateOne({}, {$set: {}})
db.<coll>.aggregate([])
Concepts
Connect
mongoshmongosh
"mongodb://<host>:27017/<db>"
mongosh
"mongodb+srv://<host>/<db>"
mongosh --host <host> --port <port> -u
<user> -p
--authenticationDatabase admin
--tls--eval "<js>" <uri>
mongosh <uri> <file.js>
exit / quit()Inspect
show dbsuse <db>dbshow collections
show usersdb.<coll>.countDocuments({})
db.<coll>.estimatedDocumentCount()
db.<coll>.stats()
db.<coll>.getIndexes()
db.stats()help / db.help() / db.<coll>.help()
Read
db.<coll>.find()
db.<coll>.find({ <field>: <value>
})
db.<coll>.findOne({ <filter> })
db.<coll>.find({}, { <f>: 1 })
db.<coll>.find().sort({ <f>: -1 })
db.<coll>.find().limit(<n>)
db.<coll>.distinct("<field>")
db.<coll>.find(<filter>).explain("executionStats")
it| Operator | Meaning |
|---|---|
$eq $ne |
equals, not equals |
$gt $gte $lt $lte |
comparison |
$in $nin |
value in / not in list |
$and $or $nor |
combine conditions |
$not |
negate an operator |
$exists |
field is present |
$type |
BSON type check |
$regex |
pattern match |
$elemMatch |
one array element matches all |
$size |
array length |
$all |
array holds every value |
$expr |
compare fields, use aggregation expressions |
Write
db.<coll>.insertOne({ <doc> })
db.<coll>.insertMany([{ }, { }])
db.<coll>.updateOne(<filter>, { $set: {
<f>: <v> } })
db.<coll>.updateMany(<filter>, { $set: { }
})
db.<coll>.replaceOne(<filter>, { <doc>
})
db.<coll>.updateOne(<filter>, <update>, {
upsert: true })
db.<coll>.deleteOne(<filter>)
db.<coll>.deleteMany(<filter>)
db.<coll>.drop()
db.dropDatabase()
| Operator | Effect |
|---|---|
$set $unset |
set or remove a field |
$inc $mul |
add to or multiply a number |
$min $max |
keep the lower or higher value |
$rename |
rename a field |
$push $addToSet |
append to array, only if new |
$pull $pop |
remove from array |
$currentDate |
set to now |
$setOnInsert |
only applied on upsert insert |
Aggregation
db.<coll>.aggregate([<stage>])
{ $match: { <filter> } }
{ $group: { _id: "$<f>", n: { $sum: 1 } }
}
{ $sort: { <f>: -1 } }
{ $project: { <f>: 1, <new>:
"$<g>" } }
{ $limit: <n> } / { $skip: <n> }
{ $lookup: { from, localField, foreignField, as } }
{ $unwind: "$<arrayField>" }
Indexes
db.<coll>.createIndex({ <f>: 1 })
db.<coll>.createIndex({ <a>: 1, <b>: -1
})
db.<coll>.createIndex({ <f>: 1 }, { unique:
true })
db.<coll>.dropIndex("<name>")
Users & Admin
db.createUser({ user: "<u>", pwd:
passwordPrompt(), roles: ["readWrite"] })
Shell & Output
printjson(<value>)
config.set("displayBatchSize", <n>)
ObjectId("<hex>")
ISODate("2026-01-01")
TabDump & Restore
mongodump --uri="<uri>"
--out=<dir>
mongodump --db=<db> --collection=<c> --gzip
--archive=<f>
mongorestore --uri="<uri>" <dir>
Recipes
db.orders.aggregate([ { $group: { _id: "$status", n: { $sum: 1 } } }, { $sort: { n: -1 } }, { $limit: 5 }])
mongosh "$MONGO_URI" --quiet \ --eval 'printjson(db.users.find({active:true}).limit(3).toArray())'
db.users.aggregate([ { $group: { _id: "$email", ids: { $push: "$_id" }, n: { $sum: 1 } } }, { $match: { n: { $gt: 1 } } }])
mongodump --uri="mongodb://old:27017/app" --gzip --archive=app.gzmongorestore --uri="mongodb://new:27017" --gzip --archive=app.gz



