MongoDB Introduction

Beginner
⏱️ 16 min read
📚 Updated: Jul 2026
🎯 5 Examples
NoSQL · BSON · mongosh

What You’ll Learn

This page is a self-contained introduction to MongoDB—a leading NoSQL database for modern applications. You will understand how document databases work, how MongoDB is structured, and how to run your first commands in mongosh.

01

What is MongoDB

NoSQL overview.

02

Data model

Docs & collections.

03

Features

Scale & flex.

04

Architecture

mongod & Atlas.

05

vs SQL

When to pick it.

06

CRUD basics

First commands.

🤔 What is MongoDB?

MongoDB is a popular NoSQL document database designed for scalability, flexibility, and high performance. Instead of storing data in rigid tables with rows and columns, MongoDB stores records as documents—JSON-like objects that can hold nested fields and arrays.

Developers use MongoDB for web APIs, mobile backends, content platforms, IoT data, catalogs, and real-time analytics. It pairs naturally with JavaScript and Node.js, but official drivers exist for most major programming languages.

💡
Beginner tip

Think of a MongoDB document like a JSON object, a collection like a folder of similar documents, and a database like a container for those collections.

🔑 Key Features of MongoDB

  • Flexible schema — documents in one collection can have different fields
  • Document model — store related data together instead of spreading across many SQL tables
  • Horizontal scalability — sharding splits data across multiple servers
  • High performance — indexes and in-memory working set for fast reads
  • Rich query language — CRUD, text search, geospatial queries, aggregation pipelines
  • Replication — replica sets provide redundancy and automatic failover
  • Cloud-ready — MongoDB Atlas offers managed hosting worldwide

🗃 Databases, Collections, and Documents

MongoDB organizes data in a hierarchy that maps closely to how developers think about objects:

MongoDBSQL equivalentDescription
DatabaseDatabaseContainer for collections (e.g. shop)
CollectionTableGroup of documents (e.g. products)
DocumentRowSingle record as BSON/JSON (e.g. one product)
FieldColumnKey-value pair inside a document (e.g. price)
_idPrimary keyUnique identifier auto-generated if omitted

Sample document

document.json
{
  "_id": ObjectId("665a1b2c3d4e5f6789012345"),
  "name": "Wireless Mouse",
  "price": 29.99,
  "tags": ["electronics", "accessories"],
  "inStock": true
}

Data is stored internally as BSON (Binary JSON), which extends JSON with extra types like Date, ObjectId, and Decimal128.

🏗 MongoDB Architecture

A typical MongoDB setup includes these components:

  • mongod — the core database server process that stores data and handles requests
  • mongosh — the modern MongoDB shell for running commands and queries interactively
  • Drivers — libraries (Node.js, Python, Java, etc.) that connect your application to MongoDB
  • Replica set — a group of mongod instances that maintain copies of the same data for availability
  • Sharded cluster — distributes collections across shards for very large datasets
  • MongoDB Atlas — fully managed cloud service with backups, monitoring, and scaling

📋 MongoDB vs SQL Databases

AspectMongoDB (NoSQL)SQL (e.g. MySQL, PostgreSQL)
Data modelDocuments in collectionsRows in tables
SchemaFlexible, evolvingFixed columns, migrations
Query languageMethods + aggregation pipelineSQL
RelationshipsEmbedding or $lookupJOINs across tables
Best forRapid iteration, nested data, horizontal scaleComplex transactions, strict relational integrity

Many production systems use both: MongoDB for flexible app data, SQL for reporting or legacy integrations.

💡 When Should You Use MongoDB?

  • Your data model changes frequently during development
  • Documents naturally represent your entities (users, orders, posts with comments)
  • You need to scale reads/writes horizontally across servers
  • You build Node.js/Express APIs and want a JavaScript-friendly database
  • You store semi-structured or heterogeneous records (logs, catalogs, IoT events)

Consider SQL instead when you need complex multi-table transactions with strict ACID rules across many related entities and a stable schema.

⚡ Quick Reference

Taskmongosh command
Show databasesshow dbs
Use databaseuse shop
Insert documentdb.products.insertOne({ name: "Pen" })
Find alldb.products.find()
Filterdb.products.find({ inStock: true })
Updatedb.products.updateOne({ name: "Pen" }, { $set: { price: 2 } })

Examples Gallery

Five starter commands for mongosh after you install MongoDB. Replace shop and products with your own names.

Example 1 — Connect and verify the server

mongosh
db.runCommand({ ping: 1 })

A successful ping confirms mongosh reached the mongod server.

Example 2 — Insert a document

mongosh
use shop
db.products.insertOne({
  name: "Notebook",
  price: 4.99,
  inStock: true
})

Example 3 — Find documents

mongosh
db.products.find({ inStock: true }).pretty()

Example 4 — Update a document

mongosh
db.products.updateOne(
  { name: "Notebook" },
  { $set: { price: 5.49 } }
)

$set updates only the specified fields and leaves other fields unchanged.

Example 5 — Delete a document

mongosh
db.products.deleteOne({ name: "Notebook" })

🧠 How MongoDB Serves a Request

1

App sends query

Your Express API or mongosh sends an operation to mongod over the wire protocol.

Request
2

mongod processes

The server parses the query, uses indexes when available, and reads/writes BSON documents.

Execute
3

Storage engine

Documents persist on disk (WiredTiger by default); recent data may live in cache for speed.

Store
=

Cursor returned

Results stream back as documents your app renders or transforms into JSON for clients.

Summary

  • MongoDB is a document-oriented NoSQL database built for flexibility and scale.
  • Data lives in databasescollectionsdocuments (BSON).
  • mongod is the server; mongosh is the shell for learning and admin tasks.
  • CRUD operations use methods like insertOne, find, updateOne, deleteOne.
  • Aggregation pipelines power analytics beyond simple finds.
  • Install locally or use MongoDB Atlas to start building immediately.

💡 Best Practices

✅ Do

  • Model data for how your application reads it (embed vs reference wisely)
  • Create indexes on fields you filter and sort by often
  • Use updateOne/deleteOne when targeting a single document
  • Validate input in your API before writing to the database
  • Enable authentication in production—never expose open mongod to the internet
  • Back up Atlas clusters or schedule mongodump for self-hosted servers

❌ Don’t

  • Store unlimited nested arrays without considering document size limits (16 MB max)
  • Run unindexed queries on large collections in production
  • Assume MongoDB has the same transactions model as every SQL database without testing
  • Put passwords or API keys directly in documents without hashing/encryption
  • Skip the Installation Guide before running examples locally
  • Design schemas only for writes—optimize for your most common read patterns too

❓ Frequently Asked Questions

MongoDB is a popular NoSQL document database that stores data as flexible JSON-like documents. It is designed for scalability, developer productivity, and high performance in modern web, mobile, and cloud applications.
No. MongoDB is a document-oriented NoSQL database. Instead of tables and rows, you work with collections and documents. You query with methods like find() and an aggregation pipeline rather than SQL SELECT statements.
It helps. The mongosh shell and Node.js driver use JavaScript syntax for queries. You can also use drivers for Python, Java, C#, Go, and other languages if you prefer.
MySQL is relational (tables, fixed schemas, SQL). MongoDB is document-based (collections, flexible schemas, BSON). MongoDB fits evolving data models; SQL fits strict relational data with complex joins across many normalized tables.
MongoDB Atlas is a managed cloud service—fast to start, free tier available, no server maintenance. Local Community Server is best for offline learning and deep admin practice. Many teams use Atlas in production.
Follow the Installation Guide, then practice insert, find, update, and delete in mongosh. Learn aggregation pipelines, indexes, and how to connect from Node.js with the official driver or Mongoose.
Did you know?

The name MongoDB comes from “humongous”—reflecting the goal of handling huge amounts of data. It was first released in 2009 and now powers applications at companies ranging from startups to global enterprises.

Try It Yourself

Install MongoDB, open mongosh, and run the CRUD examples from this page.

Start Installation Guide →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful