v6.0 Reference Guide

MongoDB CheatSheet

A complete reference guide for professional developers. Optimized for speed and performance.

Core Commands

show dbs

Lists all available databases on the server.

mongo> show dbs
use <db>

Switches context to a specific database.

mongo> use products
db

Displays the name of the current database.

mongo> db
db.stats()

Provides storage statistics for current DB.

mongo> db.stats()
db.dropDatabase()

Permanently deletes the current database.

mongo> db.dropDatabase()
db.version()

Returns the MongoDB server version.

mongo> db.version()
show collections

Lists all collections in the current DB.

mongo> show collections
db.createCollection()

Manually creates a new collection.

db.createCollection("users")

CRUD Operations

insertOne()

Adds a single document to a collection.

db.users.insertOne({name: "John"})
insertMany()

Inserts an array of documents.

db.users.insertMany([{...}, {...}])
updateOne()

Updates first matching document.

db.users.updateOne({id: 1}, {$set: {age: 25}})
updateMany()

Updates all matching documents.

db.users.updateMany({active: true}, {...})
deleteOne()

Removes a single matching document.

db.users.deleteOne({id: 1})
replaceOne()

Replaces matching document entirely.

db.users.replaceOne({id: 1}, {new: "doc"})

Query Basics

find()

Query all or filtered documents.

db.users.find({age: {$gt: 18}})
findOne()

Returns first matching document only.

db.users.findOne({email: "..."})
sort() / limit()

Order results and constrain count.

find().sort({name: 1}).limit(5)
countDocuments()

Get accurate count of filtered docs.

db.users.countDocuments({age: 20})
distinct()

Find all unique values for a field.

db.users.distinct("city")
explain()

Analyzes performance of query execution.

db.users.find().explain("executionStats")

Query Operators

Operator Definition Example
$gt / $lt Greater than / Less than comparison {age: {$gt: 21}}
$in Matches any value in the provided array {tags: {$in: ["A", "B"]}}
$and / $or Logical JOIN of multiple conditions {$or: [{age: 5}, {age: 10}]}
$elemMatch Matches criteria in array elements {scores: {$elemMatch: {$gt: 80}}}
$text Performs search on text-indexed fields {$text: {$search: "coffee"}}

Update Operators

Operator Definition Example
$set / $unset Sets field value / Removes field {$set: {status: "A"}}
$inc / $mul Increment / Multiply numeric values {$inc: {count: 1}}
$push / $pull Add or remove values from an array {$push: {tags: "New"}}
$addToSet Adds to array only if value unique {$addToSet: {tags: "unique"}}

Aggregation Pipeline

Stage Definition Example
$match Filters documents (reduces dataset) {$match: {status: "A"}}
$group Groups documents by specified ID {$group: {_id: "$cat"}}
$project Shapes documents (renaming/adding) {$project: {name: 1}}
$lookup Left outer join to another collection {$lookup: {from: "orders"}}
$unwind Deconstructs array field to documents {$unwind: "$sizes"}

Administration & Management

Command Definition Example
createIndex() Optimizes query performance db.col.createIndex({f: 1})
currentOp() Lists currently running operations db.currentOp()
watch() Opens change stream for real-time tracking db.col.watch()
hostInfo() System and host diagnostic info db.hostInfo()

mongodump / mongorestore

High-performance BSON backup and restore utilities for database migration and disaster recovery.

mongodump --db test --out /backup/

mongoexport / mongoimport

Export or import data into JSON, CSV, or TSV formats for integration with third-party tools.

mongoexport --db users --collection log --out log.json
Connect with Gerald Hinojoza