MongoDB Cheat Sheet
A searchable reference of common MongoDB shell and query API commands and what each one does.
| Command | What it does |
|---|---|
show dbs | List all databases on the server with their sizes on disk. |
use mydb | Switch to the mydb database, creating it on the first write. |
db.dropDatabase() | Delete the current database and all of its collections. |
show collections | List every collection in the current database. |
db.createCollection("users") | Create a collection named users explicitly. |
db.stats() | Show storage and object statistics for the current database. |
db.users.insertOne({ "name": "Ada", "age": 36 }) | Insert a single document into the users collection. |
db.users.insertMany([{ "name": "Ada" }, { "name": "Linus" }]) | Insert several documents in one call. |
db.users.insertOne({ "name": "Ada", "address": { "city": "London", "zip": "EC1" } }) | Insert a document that contains a nested sub-document. |
db.users.find() | Return all documents in the users collection. |
db.users.findOne() | Return a single document rather than a cursor. |
db.users.find({ "age": 36 }) | Return documents where the age field equals 36. |
db.users.find({}, { "name": 1, "_id": 0 }) | Return only the name field and hide the _id field. |
db.users.countDocuments({ "age": 36 }) | Count how many documents match a filter. |
db.users.distinct("age") | Return an array of the distinct values for a field. |
db.users.find().limit(5) | Return at most five documents. |
db.users.find().skip(10) | Skip the first ten matching documents. |
db.users.find().sort({ "age": -1 }) | Sort the results by age in descending order. |
db.users.find().pretty() | Print the results with readable indentation in the shell. |
db.users.find({ "age": { "$gt": 21 } }) | Match documents where age is greater than 21. |
db.users.find({ "age": { "$gte": 21 } }) | Match documents where age is greater than or equal to 21. |
db.users.find({ "age": { "$lt": 65 } }) | Match documents where age is less than 65. |
db.users.find({ "age": { "$lte": 65 } }) | Match documents where age is less than or equal to 65. |
db.users.find({ "age": { "$ne": 30 } }) | Match documents where age is not equal to 30. |
db.users.find({ "age": { "$in": [21, 30, 40] } }) | Match documents where age is any value in the list. |
db.users.find({ "age": { "$nin": [21, 30] } }) | Match documents where age is none of the listed values. |
db.users.find({ "$and": [{ "age": { "$gt": 21 } }, { "active": true }] }) | Match documents that satisfy every listed condition. |
db.users.find({ "$or": [{ "age": 21 }, { "age": 30 }] }) | Match documents that satisfy at least one condition. |
db.users.find({ "age": { "$not": { "$gt": 21 } } }) | Match documents where the inner condition is false. |
db.users.find({ "email": { "$exists": true } }) | Match documents that contain the email field. |
db.users.find({ "age": { "$type": "int" } }) | Match documents where a field has the given BSON type. |
db.users.find({ "name": { "$regex": "^A" } }) | Match documents where a string field matches a regular expression. |
db.users.find({ "scores": { "$elemMatch": { "$gte": 5 } } }) | Match arrays that have an element meeting all inner conditions. |
db.users.updateOne({ "name": "Ada" }, { "$set": { "age": 37 } }) | Update only the first document that matches the filter. |
db.users.updateMany({ "active": false }, { "$set": { "active": true } }) | Update every document that matches the filter. |
db.users.updateOne({ "_id": 1 }, { "$set": { "city": "Paris" } }) | Set or overwrite the value of one or more fields. |
db.users.updateOne({ "_id": 1 }, { "$unset": { "city": "" } }) | Remove a field from the matching document. |
db.users.updateOne({ "_id": 1 }, { "$inc": { "age": 1 } }) | Increase or decrease a numeric field by an amount. |
db.users.updateOne({ "_id": 1 }, { "$push": { "tags": "vip" } }) | Append a value to an array field. |
db.users.updateOne({ "_id": 1 }, { "$pull": { "tags": "vip" } }) | Remove all matching values from an array field. |
db.users.updateOne({ "_id": 1 }, { "$addToSet": { "tags": "vip" } }) | Add a value to an array only if it is not already present. |
db.users.updateOne({ "email": "a@x.com" }, { "$set": { "age": 1 } }, { "upsert": true }) | Update the match, or insert a new document when none matches. |
db.users.replaceOne({ "_id": 1 }, { "name": "Ada", "age": 37 }) | Replace an entire document while keeping its _id. |
db.users.deleteOne({ "name": "Ada" }) | Delete the first document that matches the filter. |
db.users.deleteMany({ "active": false }) | Delete every document that matches the filter. |
db.orders.aggregate([{ "$match": { "status": "shipped" } }]) | Run an aggregation pipeline over a collection. |
{ "$match": { "status": "shipped" } } | Pipeline stage that filters documents like a query. |
{ "$group": { "_id": "$customer", "total": { "$sum": "$amount" } } } | Pipeline stage that groups documents and computes totals. |
{ "$sort": { "total": -1 } } | Pipeline stage that orders the documents flowing through it. |
{ "$project": { "name": 1, "total": 1, "_id": 0 } } | Pipeline stage that selects or reshapes the output fields. |
{ "$lookup": { "from": "customers", "localField": "customerId", "foreignField": "_id", "as": "customer" } } | Pipeline stage that joins another collection. |
db.orders.createIndex({ "customerId": 1 }) | Create an index on a field to speed up queries and sorts. |
No commands match your search.
Runs entirely in your browser. Nothing you type is sent anywhere; open DevTools and watch the Network tab to verify zero requests.
What this tool does
This is a searchable quick reference for the MongoDB commands you use in the mongosh shell and the query API, from creating databases and inserting documents to querying, filtering with operators, updating, deleting, and building aggregation pipelines. Each row pairs an exact command with a plain-English note on what it does. Type in the filter box to search across both columns, or tap a category button to focus on one area. The whole list is built into the page, so it works offline and sends nothing anywhere.
How to use it
Start typing in the Filter box. Entering find surfaces
every read query; entering update or $set shows the write
operators; entering aggregate jumps to the pipeline stages. The
category buttons (Databases, Insert, Query, Operators, Update, Delete
and aggregate) narrow the table to one family and combine with the text filter, so you
can pick Operators and type array to zero in on the array matchers. Clear
the box to see the full sheet again.
Common use cases
- Recalling the exact syntax for a command you use rarely, like
$elemMatchor$lookup. - Copying a working query into mongosh instead of guessing the field order.
- Teaching a teammate the difference between
updateOneandupdateMany. - Checking which operator you need, such as
$inversus$elemMatch. - Reviewing the core MongoDB query API before an interview or a new project.
Common pitfalls
- Updating too many documents.
updateManytouches every match, so a loose filter can rewrite a whole collection. Test the filter withfindfirst to see exactly what it selects. - Replacing instead of updating.
replaceOneswaps the whole document and drops any fields you did not list, while a plain object without update operators is rejected byupdateOne. Reach for$setwhen you only mean to change some fields. - Missing indexes. A query on an unindexed field scans every document,
which is slow on large collections. Add an index with
createIndexfor the fields you filter or sort on most often. - Confusing push and addToSet.
$pushalways appends and can create duplicate array entries, while$addToSetadds a value only when it is missing. Pick the one that matches whether duplicates are allowed.
Frequently asked questions
- What is a document and a collection in MongoDB?
- A document is a single record stored as a set of field and value pairs, much like a JSON object, and it is the basic unit of data in MongoDB. A collection is a group of documents, roughly the equivalent of a table in a relational database, except that documents in the same collection do not have to share the same fields. Databases hold collections, and collections hold documents.
- What is the difference between updateOne and updateMany?
- updateOne changes only the first document that matches the filter, even when many documents would match. updateMany changes every document that matches the filter. Both accept the same update operators, such as $set and $inc, so the only difference is how many documents are affected.
- What does upsert do?
- Upsert is short for update-or-insert. When you pass the upsert option as true, MongoDB updates the document that matches your filter if one exists, and otherwise inserts a brand new document built from the filter and the update. It is handy for save-or-create logic where you do not know in advance whether the record already exists.
- How does the aggregation pipeline work?
- An aggregation pipeline is an ordered list of stages, where each stage transforms the stream of documents and passes the result to the next stage. Common stages filter with $match, group and total with $group, reorder with $sort, and reshape fields with $project. Because each stage feeds the next, you can build reports, joins, and calculations that a single find query cannot express.
- What is $lookup used for?
- $lookup is the aggregation stage that performs a left outer join to another collection in the same database. You tell it the collection to join from, the local field, the foreign field to match against, and the name of the array field that will hold the matching documents. It lets you combine related data, such as attaching each order to its customer, without running a separate query.
- Does this cheat sheet send my searches anywhere?
- No. The entire command list is baked into the page and every search and filter runs in your browser with JavaScript, so nothing you type leaves your device. Open your browser DevTools and watch the Network tab while you search to confirm there are zero requests.
Cite this tool
For academic, journalistic, or technical references. Pick a format:
Citations use 2026 as the publication year. Access date is left as a fillable placeholder where the citation style expects one.