MongoDB Basics
Learn MongoDB document model, collections, BSON types, and local setup.
Document-Oriented Data Model
MongoDB stores data as flexible BSON documents rather than fixed rows and columns. Each document maps naturally to objects in programming languages, which reduces the impedance mismatch between application code and database storage.
Documents can contain nested objects and arrays, so you can model one-to-many relationships without always needing separate collections or join tables. This flexibility suits evolving schemas, but you still need to design document structure deliberately to avoid duplication and unbounded array growth.
- Documents live in collections (similar to tables in SQL)
- Every document has a unique _id field (auto-generated if omitted)
- Schema is flexible by default; enforce structure with validation rules when needed
// A document in a users collection
{
_id: ObjectId("507f1f77bcf86cd799439011"),
name: "Ada Lovelace",
email: "ada@example.com",
roles: ["admin", "editor"],
profile: { country: "UK", joined: ISODate("2024-01-15") }
}Databases and Collections
A MongoDB server hosts multiple databases, and each database contains collections of documents. Unlike SQL, you do not need to define columns upfront—collections grow organically as you insert documents with varying shapes.
Use meaningful collection names that reflect domain entities (users, orders, products). Keep related data in the same collection when documents are queried together, and split into separate collections when access patterns or lifecycle differ significantly.
- One MongoDB instance can host many databases
- Collections are created implicitly on first insert if they do not exist
- Use db.getCollectionNames() from drivers to enumerate collections programmatically
use myapp
db.createCollection("users")
db.createCollection("orders")
// List collections in the current database
show collectionsBSON Types and Field Naming
BSON extends JSON with additional types such as ObjectId, Date, Decimal128, and Binary. Choosing the right type at write time prevents subtle bugs during queries and aggregation.
Field names should be lowercase with underscores or camelCase consistently across your codebase. Avoid dots in field names unless you intentionally want nested path semantics, because dots are interpreted as path separators in update operators.
db.products.insertOne({
sku: "SKU-1001",
price: Decimal128("29.99"),
tags: ["sale", "featured"],
createdAt: new Date(),
metadata: { warehouse: "EU-1" }
})Installation and Connection
Install MongoDB Community Server locally for development, or use MongoDB Atlas for managed cloud hosting. The mongosh shell is the primary tool for interactive exploration and administration.
Application drivers connect via a connection URI that specifies hosts, credentials, and options such as replica set name or TLS. Always use connection pooling in production applications rather than opening a new connection per request.
- Use mongosh for ad-hoc queries and index inspection
- Store connection strings in environment variables, never in source code
- Enable TLS for any network-accessible deployment
// Node.js driver connection
import { MongoClient } from "mongodb";
const client = new MongoClient(process.env.MONGODB_URI!);
await client.connect();
const db = client.db("myapp");
const users = db.collection("users");When MongoDB Fits Best
MongoDB excels when your data model is document-shaped, schemas evolve frequently, or you need horizontal scaling with sharding. It is a strong choice for catalogs, content management, IoT telemetry, and mobile backends.
It is less ideal when you require complex multi-table joins as the primary access pattern, strict relational integrity without application logic, or heavy analytical workloads that benefit from columnar storage. Match the database to your read/write patterns, not hype.
- Prefer embedding when data is read together and has a bounded size
- Prefer referencing when related documents change independently or grow without limit
- Design indexes early based on query patterns, not after performance problems appear