1) Getting Started & Connection
These are the basic commands you use to enter MongoDB and understand where you are working.
mongosh starts the MongoDB shell. You can think of it as opening a terminal that talks directly to your database.
If you want to be explicit about which server you’re connecting to, you can use
mongosh “mongodb://localhost:27017”.
This is useful when you have more than one MongoDB server or environment.
When you’re done, exit closes the MongoDB shell, similar to closing a terminal session.
2) Database Basics
These commands help you explore and move between databases.
show dbs lists all databases available on the server. Keep in mind that empty databases won’t show up until they contain data.
use myDatabase switches you to a database named myDatabase. MongoDB creates it automatically the moment you insert data.
Typing db simply tells you which database you’re currently using, which is helpful if you’re jumping around a lot.
3) Collections (Like Tables)
Collections are where your data actually lives.
show collections displays all collections inside the current database.
db.createCollection(“users”) explicitly creates a collection. This step is optional, but it’s useful when you want a clear structure from the start.
db.users.drop() deletes the entire collection along with all its data. There’s no undo here, so this command should be used carefully.
4) Inserting Data
MongoDB stores information as documents, which look very similar to JSON objects.
db.users.insertOne({ name: “Kia”, age: 40 }) inserts a single document into the users collection. This is the most straightforward way to add data.
db.users.insertMany([{ name: “Ali” }, { name: “Sara” }]) lets you insert multiple documents at once and is much faster for bulk inserts.
If you don’t specify an _id, MongoDB automatically generates one for every document.
5) Reading Data (Queries)
This is how you retrieve data from your collections.
db.users.find() returns all documents in the users collection.
db.users.find({ age: 40 }) returns only documents that match the condition you provide.
db.users.findOne({ name: “Kia” }) returns just the first matching document.
db.users.find().pretty() formats the output so it’s easier to read.
6) Updating Data
Updates allow you to modify existing documents.
db.users.updateOne({ name: “Kia” }, { $set: { age: 41 } }) updates a single matching document.
db.users.updateMany({ age: { $gt: 30 } }, { $set: { active: true } }) updates all documents that match the condition.
db.users.replaceOne({ name: “Kia” }, { name: “Kia”, age: 42 }) replaces the entire document, so it should be used with caution.
7) Deleting Data
These commands remove data from your database.
db.users.deleteOne({ name: “Ali” }) deletes a single document.
db.users.deleteMany({ age: { $lt: 18 } }) deletes all documents that meet the condition.
db.users.remove({}) is deprecated and should be avoided in new projects.
8) Filtering & Operators
Operators help you ask more precise questions.
Comparison operators like $gt, $lt, $gte, and $lte let you work with ranges.
$in and $nin check whether a value exists inside or outside a list.
$and and $or allow you to combine multiple conditions.
For example:
db.users.find({ age: { $gte: 30, $lte: 50 } })
9) Sorting, Limiting & Counting
These commands help clean up your query results.
db.users.find().sort({ age: -1 }) sorts results by age in descending order.
db.users.find().limit(5) shows only the first five documents.
db.users.countDocuments() accurately counts how many documents exist in a collection.
10) Indexes (Performance Boost)
Indexes make queries much faster, especially as your data grows.
db.users.createIndex({ email: 1 }) creates an index on the email field.
db.users.getIndexes() shows all indexes on the collection.
db.users.dropIndex(“email_1”) removes an index you no longer need.
11) Aggregation (Data Analysis)
Aggregation is used for reports, statistics, and grouped data.
db.orders.aggregate([…]) runs a pipeline of data processing steps.
Common stages include filtering data with $match, grouping it with $group, reshaping it with $project, and ordering results with $sort.
This is MongoDB’s equivalent of analytical SQL queries.
12) Users & Security (Basic)
These commands help manage database access.
db.createUser({ user, pwd, roles }) creates a new database user.
show users lists all users in the current database.
db.dropUser(“username”) removes a user.
13) Backup & Restore (Terminal Level)
These commands are run outside the MongoDB shell.
mongodump creates a backup of your database.
mongorestore restores data from an existing backup.
These are essential for production environments.
14) Debugging & Info
When something feels wrong, these commands provide insight.
db.stats() shows overall database statistics.
db.users.stats() gives details about a specific collection.
db.serverStatus() provides deep information about server health and performance.
Final Tip (Real-Life Use)
In day-to-day app development—especially with a SwiftUI + Node + MongoDB stack—you’ll regularly use only a small set of commands: find, insertOne, updateOne, deleteOne, and createIndex.