Set MongoDB database and collection boundaries from ownership, access patterns, lifecycle, indexes, and operational responsibility.
A MongoDB server can host multiple databases. Each database has its own set of collections and is isolated from others. MongoDB creates a database automatically the first time you insert data into it - there is no explicit CREATE DATABASE command needed.
// List all databases (only shows databases with data)
show dbs
// Switch to a database (creates it on first write)
use myapp
// Show current database name
db
// Get database statistics
db.stats()
// Drop the current database (irreversible!)
db.dropDatabase()
// Copy a database (deprecated in newer versions - use mongodump/mongorestore)
// db.copyDatabase("source", "destination")
Collections are analogous to tables in relational databases. They hold groups of documents. Collections are created implicitly when you first insert a document, or explicitly using createCollection() when you need to set specific options like capped collections or validation rules.
use myapp
// Implicit creation - collection is created on first insert
db.users.insertOne({ name: "Alice" })
// Explicit creation with options
db.createCollection("products")
// List all collections in current database
show collections
// or
db.getCollectionNames()
// Get collection info (includes options and UUID)
db.getCollectionInfos({ name: "users" })
// Rename a collection
db.users.renameCollection("customers")
// Drop a collection (removes all documents and indexes)
db.products.drop()
// Get collection statistics
db.users.stats()
db.users.totalSize()
db.users.storageSize()
Capped collections are fixed-size collections that automatically overwrite the oldest documents when the size limit is reached. They are ideal for logs, caches, and event streams where you only need the most recent data.
// Create a capped collection
// size: max bytes (required), max: max number of documents (optional)
db.createCollection("appLogs", {
capped: true,
size: 10485760, // 10 MB
max: 5000 // max 5000 documents
})
// Check if a collection is capped
db.appLogs.isCapped() // true
// Create a collection with a collation (language-specific sorting)
db.createCollection("articles", {
collation: { locale: "en", strength: 2 }
})
// Convert an existing collection to capped
db.runCommand({
convertToCapped: "logs",
size: 5242880 // 5 MB
})
A MongoDB database groups collections for one application, service, or domain. A collection groups documents that usually represent the same kind of thing, such as students, orders, products, or events. Unlike relational tables, documents in a collection can vary in shape, but a consistent design is still important for queries and maintenance.
Good collection design starts from how the application reads and writes data. If a screen always loads an order with its line items, embedding line items inside the order document may be natural. If data grows independently or is shared across many places, referencing may be better. Database and collection names should make ownership and purpose obvious.
use school_app
db.students.insertOne({
name: 'Asha',
className: '10A',
subjects: ['Math', 'Science']
})
db.students.find({ className: '10A' })
Create it first when you need validation, a capped size, collation, or other options.
No. db.dropDatabase() takes effect immediately on the selected database.
A fixed-size collection that keeps insertion order and overwrites old data when full.
Explore 500+ free tutorials across 20+ languages and frameworks.