Comprehensive MongoDB Command Reference (2026 Edition)
This article provides a complete, step‑by‑step guide to MongoDB shell commands—including show dbs, use, db, collection operations, indexing, aggregation and statistics—complete with syntax, example outputs and practical usage notes for developers and administrators.
show dbs
Lists all databases in the current MongoDB instance. show dbs Example output:
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
order 2.3 GiB
user 1.1 GiBUseful for quickly viewing the database list.
use
Switches to a specified database; if the database does not exist, it is created on the first write. use order Output: switched to db order Creating a database occurs only after inserting data.
db
Displays the name of the current database. db Output:
orderCommonly used during development and debugging.
show collections
Lists all collections in the current database. show collections Output example:
user
order
product
logSimilar to MySQL's
SHOW TABLES;db.createCollection
Creates a new collection. db.createCollection("user") Output: { ok : 1 } With options (e.g., capped collection):
db.createCollection("log", { capped: true, size: 104857600 })Creates a fixed‑size log collection.
db.collection.insertOne
Inserts a single document into a collection.
db.user.insertOne({ name: "Tom", age: 25, city: "Beijing" })Output:
{ acknowledged: true }Suitable for adding a single record.
db.collection.insertMany
Inserts multiple documents into a collection.
db.user.insertMany([
{ name: "Tom", age: 20 },
{ name: "Lucy", age: 22 }
])Output:
{ acknowledged: true }Useful for initializing data.
db.collection.find
Retrieves documents; without parameters returns all. db.user.find() Formatted output: db.user.find().pretty() Query specific fields: db.user.find({}, { name: 1, age: 1 }) Conditional query example:
db.user.find({ age: 20 })db.collection.findOne
Returns one matching document. db.user.findOne({ name: "Tom" }) Output example:
{ "_id": "...", "name": "Tom", "age": 20 }db.collection.updateOne
Modifies a single matching document.
db.user.updateOne({ name: "Tom" }, { $set: { age: 30 } })Update multiple documents:
db.user.updateMany({}, { $set: { status: 1 } })db.collection.deleteOne
Removes a single matching document. db.user.deleteOne({ name: "Tom" }) Delete multiple documents with a condition: db.user.deleteMany({ age: { $lt: 18 } }) Delete all documents:
db.user.deleteMany({})db.collection.countDocuments
Returns the number of documents in a collection. db.user.countDocuments() Conditional count example:
db.user.countDocuments({ age: { $gte: 18 } })Useful for pagination statistics.
db.collection.createIndex
Creates a standard index. db.user.createIndex({ name: 1 }) Compound index example:
db.order.createIndex({ userId: 1, createTime: -1 })Unique index example:
db.user.createIndex({ email: 1 }, { unique: true })View indexes:
db.user.getIndexes()db.collection.aggregate
Performs advanced data aggregation.
Example – count orders by status:
db.order.aggregate([
{ $group: { _id: "$status", total: { $sum: 1 } } }
])Example – count documents per age:
db.user.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } }
])db.stats
Shows storage statistics for the current database. db.stats() Typical fields returned: collections, objects, storageSize, indexSize, dataSize.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
