MongoDB — Databases & Collections Management

🍃 MongoDB 7.0+ 🟢 Chapter 6 of 50 📂 Phase 02: Databases, Collections & BSON Documents 📅 2026 Edition

Learn how to create, list, and drop databases and collections in MongoDB. Understand implicit creation, explicit creation options, and capped collections for circular logging.

1Managing Databases in MongoDB

In MongoDB, databases are namespaces that group collections together. Unlike SQL, you do not need to issue a CREATE DATABASE command before inserting data.

Database Commands
// 1. Switch to a database (creates it in memory if it doesn't exist)
use store_db

// 2. Show databases (only databases containing at least 1 document are listed!)
show dbs

// 3. Drop the active database
db.dropDatabase()
// Output: { ok: 1, dropped: "store_db" }
2Implicit vs Explicit Collection Creation

MongoDB supports two ways to create a collection:

  • Implicit Creation: Simply insert a document into a non-existent collection name. MongoDB automatically creates the collection on-the-fly!
  • Explicit Creation: Use db.createCollection(name, options) when you need custom collection options like validation or capping.
Collection Examples
// Implicit creation:
db.customers.insertOne({ name: "Alice", score: 95 });

// Explicit creation with options:
db.createCollection("products", {
  capped: false,
  storageEngine: { wiredTiger: {} }
});
3Capped Collections (Fixed-Size Circular Buffers)

A Capped Collection is a fixed-size collection that works like a circular FIFO (First-In, First-Out) buffer. When the specified maximum size or document limit is reached, MongoDB automatically overwrites the oldest documents with new ones!

Creating a Capped Log Collection
// Create a capped collection of max 5 MB or 1,000 documents
db.createCollection("system_logs", {
  capped: true,
  size: 5242880, // 5 MB in bytes
  max: 1000      // max 1000 docs
});

// Check if a collection is capped:
db.system_logs.isCapped() // returns true
4Collection Management Commands Cheat Sheet
mongosh Commands
// List collections in active database
show collections

// Rename a collection
db.customers.renameCollection("users")

// Drop a collection completely (deletes all documents & indexes)
db.users.drop()
OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on MongoDB 7.0+ Standards · Last updated August 2026