MongoDB Databases and Collections

Every piece of data in MongoDB lives inside a three-level structure. A database holds collections, and a collection holds documents. Knowing this structure helps you organize data cleanly from the first day.

The Filing Cabinet Picture

Picture an office with one large filing cabinet. The cabinet is the database. Each drawer inside the cabinet is a collection, such as Customers or Orders. Each paper inside a drawer is a document that describes one customer or one order.

Database: shop
  |
  +-- Collection: customers
  |      +-- Document: { name: "Asha",  city: "Pune" }
  |      +-- Document: { name: "Rohit", city: "Delhi" }
  |
  +-- Collection: orders
         +-- Document: { item: "Pen",  qty: 10 }
         +-- Document: { item: "Book", qty: 2 }

Comparison with Relational Terms

MongoDB TermFamiliar Term
DatabaseDatabase
CollectionTable
DocumentRow
FieldColumn

A collection differs from a table in one important way. Documents in the same collection can carry different fields, so one customer can have a phone number while another has none.

Work with Databases

Create and Switch

MongoDB has no separate command to create a database. The use command switches to a database name, and MongoDB builds the database when the first document arrives.

use school
db.students.insertOne({ name: "Meera", grade: 8 })

List and Check

show dbs
db

The first command lists databases that hold data. The second command prints the name of the database you currently use. An empty database does not appear in the list.

Delete a Database

use school
db.dropDatabase()

This command removes the current database with all its collections and documents. Deleted data does not return, so check the database name before you press Enter.

Built-in Databases

NamePurpose
adminStores user accounts and administrator roles
localStores internal server data, such as the replication log
configStores settings for sharded clusters

Leave these three databases alone unless you manage the server.

Work with Collections

Create a Collection Automatically

A collection appears the moment you insert a document into it:

db.books.insertOne({ title: "Learn MongoDB", pages: 250 })

Create a Collection on Purpose

The createCollection method builds an empty collection and accepts options:

db.createCollection("reviews")

Capped Collections

A capped collection has a fixed size. When it fills up, MongoDB deletes the oldest documents to make room for new ones. Think of a security camera that records over old footage.

db.createCollection("logs", { capped: true, size: 1048576, max: 1000 })

The size option sets the limit in bytes, and the max option sets the highest number of documents. Capped collections suit log records and recent activity feeds.

List, Rename, and Delete

show collections
db.books.renameCollection("library")
db.library.drop()
  • show collections lists every collection in the current database.
  • renameCollection changes the name and keeps all documents.
  • drop removes the collection and returns true when it succeeds.

Check Collection Size

db.students.countDocuments()
db.students.stats()

The countDocuments method returns the number of documents. The stats method reports storage size and index size.

Naming Rules

Database Names

  • Avoid spaces, dots, slashes, and quotation marks.
  • Keep the name under 64 characters.
  • Use lowercase letters, because names are case-sensitive on most systems.

Collection Names

  • Start with a letter or an underscore.
  • Skip the dollar sign and the empty string.
  • Avoid the prefix system., which MongoDB reserves.
  • Choose plural nouns such as students and orders for clarity.

Organizing Tips

  • Create one database for each application.
  • Group documents that applications read together into one collection.
  • Keep different kinds of data in separate collections, such as users and invoices.
  • Pick short, consistent names so teammates find data quickly.

Summary

MongoDB stores data in databases, collections, and documents. A database groups collections, and a collection groups related documents. MongoDB creates both automatically on the first insert, and the drop commands remove them permanently. Capped collections keep a fixed amount of recent data. Clear names and a tidy structure make every later topic easier to learn.

Leave a Comment

Your email address will not be published. Required fields are marked *