Commands Cheat Sheet

Evaluating engineering tools? Get the comparison in Google Sheets

(Perfect for making buy/build decisions or internal reviews.)

Most-used commands
Your email is safe thing.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

Connection

mongo
Start the MongoDB shell client

mongo --host --port
Connect to a MongoDB instance

mongo "mongodb://:/"
Connect with connection string

mongo --username --password --authenticationDatabase
Connect with authentication

use
Switch to a specific database

Database Operations

show dbs
List all databases

show collections
List all collections in current database

db.stats()
Display statistics for current database

db.dropDatabase()
Delete the current database

Collection Operations

db.createCollection("")
Create a new collection

db..stats()
Show statistics for a collection

db..drop()
Delete a collection

CRUD Operations

db..find()
Query documents in a collection

db..find().pretty()
Format query results for readability

db..findOne()
Retrieve first matching document

db..insertOne({})
Insert a single document

db..insertMany([{}, {}])
Insert multiple documents

db..updateOne({query}, {$set: {}})
Update a single document

db..updateMany({query}, {$set: {}})
Update multiple documents

db..deleteOne({query})
Delete a single document

db..deleteMany({query})
Delete multiple documents

Query Operations

db..find({field: value})
Filter documents by field value

db..find({field: {$gt: value}})
Greater than comparison

db..find({field: {$lt: value}})
Less than comparison

db..find().sort({field: 1})
Sort results (1 for ascending, -1 for descending)

db..find().limit(n)
Limit results to n documents

db..find().skip(n)
Skip n documents in results

db..count()
Count documents in collection

db..distinct("field")
Get distinct values for a field

Indexing

db..createIndex({field: 1})
Create an index (1 for ascending, -1 for descending)

db..createIndex({field: "text"})
Create a text index

db..getIndexes()
List all indexes on a collection

db..dropIndex("index_name")
Remove an index

Aggregation

db..aggregate([{$match: {}}, {$group: {}}])
Run an aggregation pipeline

db..mapReduce(map, reduce, {out: "collection"})
Perform map-reduce operation

Monitoring

db.serverStatus()
Get server status information

db.stats()
Current database statistics

db.currentOp()
View current operations

db.killOp(opId)
Kill a specific operation by ID

rs.status()
Check replica set status

Administrative

db.shutdownServer()
Shutdown the MongoDB server

db.runCommand({command})
Run a database command

db.createUser()
Create a new user

db.getUsers()
List users in current database

db.auth("username", "password")
Authenticate as user