Core Commands
Lists all available databases on the server.
mongo> show dbs
Switches context to a specific database.
mongo> use products
Displays the name of the current database.
mongo> db
Provides storage statistics for current DB.
mongo> db.stats()
Permanently deletes the current database.
mongo> db.dropDatabase()
Returns the MongoDB server version.
mongo> db.version()
Lists all collections in the current DB.
mongo> show collections
Manually creates a new collection.
db.createCollection("users")
CRUD Operations
Adds a single document to a collection.
db.users.insertOne({name: "John"})
Inserts an array of documents.
db.users.insertMany([{...}, {...}])
Updates first matching document.
db.users.updateOne({id: 1}, {$set: {age: 25}})
Updates all matching documents.
db.users.updateMany({active: true}, {...})
Removes a single matching document.
db.users.deleteOne({id: 1})
Replaces matching document entirely.
db.users.replaceOne({id: 1}, {new: "doc"})
Query Basics
Query all or filtered documents.
db.users.find({age: {$gt: 18}})
Returns first matching document only.
db.users.findOne({email: "..."})
Order results and constrain count.
find().sort({name: 1}).limit(5)
Get accurate count of filtered docs.
db.users.countDocuments({age: 20})
Find all unique values for a field.
db.users.distinct("city")
Analyzes performance of query execution.
db.users.find().explain("executionStats")
Query Operators
Update Operators
Aggregation Pipeline
Administration & Management
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