320 x 50 Mobile Ad
Cheat Sheets

MongoDB Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

Database

use <db_name>

Switch to or create a database

show dbs

List all databases

db

Show current database

db.dropDatabase()

Drop the current database

db.stats()

Get database statistics

Collection

show collections

List all collections in current database

db.createCollection('name')

Explicitly create a collection

db.coll.drop()

Drop the specified collection

db.coll.stats()

Get collection statistics

db.coll.renameCollection('new')

Rename a collection

Create

db.coll.insertOne({ k: 'v' })

Insert a single document into collection

db.coll.insertMany([{k:'v'}, {k:'w'}])

Insert multiple documents

Read (Queries)

db.coll.find()

Find all documents in collection

db.coll.findOne()

Find a single document

db.coll.find({ k: 'v' })

Find documents matching condition

db.coll.find().pretty()

Format the output for readability

db.coll.find({ k: { $gt: 5 } })

Find documents where k > 5

db.coll.find({ k: { $in: [1, 2] } })

Find documents where k is in array

db.coll.find({ k: /pattern/ })

Query using regular expressions

db.coll.find({ $or: [{a:1}, {b:2}] })

Find documents matching either condition

db.coll.find({}, { name: 1, _id: 0 })

Projection: Return only 'name' field, exclude '_id'

Update

db.coll.updateOne({ k: 'v' }, { $set: { c: 1 } })

Update first matching document

db.coll.updateMany({ k: 'v' }, { $set: { c: 1 } })

Update all matching documents

db.coll.replaceOne({ k: 'v' }, { new: 'doc' })

Replace the entire document

db.coll.updateOne({ k:'v' }, { $inc: { count: 1 } })

Increment a numeric field

db.coll.updateOne({ k:'v' }, { $unset: { c: 1 } })

Remove a field from document

db.coll.updateOne({ k:'v' }, { $push: { arr: 1 } })

Append value to an array

db.coll.updateOne({ k:'v' }, { $pull: { arr: 1 } })

Remove value from an array

db.coll.updateOne(query, update, { upsert: true })

Update or insert if document doesn't exist

Delete

db.coll.deleteOne({ k: 'v' })

Delete first document matching query

db.coll.deleteMany({ k: 'v' })

Delete all documents matching query

db.coll.deleteMany({})

Delete all documents in collection (truncate)

Modifiers

db.coll.find().count()

Count the number of documents returned

db.coll.find().limit(5)

Limit the number of returned documents

db.coll.find().skip(5)

Skip the first 5 documents

db.coll.find().sort({ k: 1 })

Sort results (1 for ascending, -1 for descending)

Aggregation

db.coll.aggregate([{ $match: { k: 'v' } }])

Filter documents in pipeline

db.coll.aggregate([{ $group: { _id: '$cat', total: { $sum: 1 } } }])

Group documents and calculate sum

db.coll.aggregate([{ $sort: { total: -1 } }])

Sort grouped results

db.coll.aggregate([{ $project: { name: 1, upper: { $toUpper: '$name' } } }])

Reshape documents and add fields

Indexes

db.coll.createIndex({ k: 1 })

Create an index on field 'k'

db.coll.createIndex({ k: 1 }, { unique: true })

Create a unique index

db.coll.getIndexes()

List all indexes on collection

db.coll.dropIndex('k_1')

Drop a specific index

Administration

db.createUser({ user: 'u', pwd: 'p', roles: ['readWrite'] })

Create a new user with roles

db.auth('user', 'pwd')

Authenticate with database

db.currentOp()

View currently running operations

db.killOp(opid)

Kill a running operation

Advanced

Advanced Command Snippet

Explore advanced configurations for Mongo

System Optimization

Performance tuning best practices for Mongo

Security Audit

Run security checks and validation for Mongo

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Mongo Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Mongo commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Mongo and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Mongo commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Mongo features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Mongo reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Mongo operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online