If you‘re a developer or data professional looking to learn MongoDB, you‘ve come to the right place! As someone who has worked with MongoDB for over 5 years, I‘m excited to share my practical knowledge with you in this complete beginner‘s guide.
In this lengthy tutorial, we‘ll start by understanding "why MongoDB", then quickly move on to download, install and get started with simple CRUD operations. After that, we‘ll explore advanced querying, indexing, aggregation and many powerful features of MongoDB.
By the end, you‘ll have all the knowledge needed to start building apps and analyzing data using MongoDB! Let‘s get started.
Why Use MongoDB?
Before we jump into MongoDB specifics, let‘s first understand what problem MongoDB solves.
Traditionally, web and mobile applications used RDBMS like MySQL to store structured data. But as apps become more dynamic and deal with unstructured data like social posts, IoT sensor data, etc traditional relational databases become less optimal.
MongoDB represents a new generation of non-relational databases optimized for scalability, flexibility and performance. Some key advantages of MongoDB over legacy RDBMS:
- Flexible data model using JSON-style documents
- Horizontally scalable across servers for high availability
- Tuned for high performance – low latency and high throughput
- Rich querying and data aggregation capabilities
Let‘s explore each benefit in more detail:
Flexible Schema using Dynamic Documents
MongoDB stores data in flexible JSON-like documents rather than rows and columns. This allows storing heterogeneous and nested data together while keeping a clean data model.
For example, if we are storing product data in MongoDB, a single product document can contain complete info including name, brand, variants, tags, images, user reviews and even analytics events in a single structure. The flexible document model handles evolving data requirements much more easily compared to rigid SQL schema.
Scales Horizontally for High Availability
SQL databases scale vertically by increasing hardware resources of a single server. But that has limits. MongoDB scales horizontally on commodity hardware using sharding. Data is distributed across many nodes for parallel processing.
MongoDB also replicates data automatically across multiple servers to provide high availability and durability. If a server goes down, other members of the replica set ensure the database stays up.
This makes MongoDB highly scalable and reliable for demanding workloads. Companies like eBay, Walmart and META use MongoDB to handle billions of requests daily.
Optimized for Speed and Performance
MongoDB uses a binary JSON format called BSON to store data. Along with a document storage engine called WiredTiger, it provides indexing and compression capabilities for faster reads and writes.
The query layer is also optimized to provide low latency along with parallel processing of data using multi-threaded operations. MongoDB can leverage all resources of a machine using powerful aggregation methods like:
- $lookup for joins
- $graphLookup for graph queries
- $geoNear for geospatial queries
- $facet for grouping and filtering
This combination of performance, robust indexing and native aggregation make MongoDB a great fit for real-time apps.
With MongoDB basics covered, let‘s get hands-on now! I‘ll be using MongoDB version 4.2 in this guide but most concepts apply across versions.
Installation and Setup
MongoDB supports all major operating systems. We‘ll look at how to install and run MongoDB server on Mac, Windows and Linux.
Install MongoDB on Mac
On macOS, Homebrew provides the easiest installation method:
brew tap mongodb/brew
brew install mongodb-community
This downloads the latest stable version. Alternatively, you can grab the tarball and extract it.
After installing, create the default data directory:
mkdir -p /data/db
Install MongoDB on Windows
On Windows, download the msi installer from mongodb.com/downloads. Run the installer exe and follow the setup wizard.
You can accept the default installation path like C:\Program Files\MongoDB\Server\4.2\ or customize it as needed. The data directory can be something like C:\data\db.
Install MongoDB on Linux
For Debian/Ubuntu systems, install MongoDB with apt:
sudo apt-get install mongodb
For RHEL, CentOS, Fedora and related distros, enable the MongoDB repo and install:
sudo yum install mongodb-org
For other Linux distributions, grab the tarball from mongodb.com and extract it to a directory like /opt/mongodb.
Before starting the server, create the data and log directories:
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
Starting MongoDB Server
With MongoDB installed, we can start the Mongo server process.
On macOS or Linux:
mongod --dbpath /data/db
On Windows:
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath C:\data\db
This will start mongod process and store data files in the dbpath. You should see a lot of log messages when MongoDB starts up successfully.
By default, MongoDB runs on port 27017 and listens for connections from clients.
Let‘s connect!
Connecting to MongoDB from Shell
The mongo shell allows interacting with MongoDB directly from command line. It‘s great for testing and administration.
To connect, simply run:
mongo
This will connect to the mongod instance running on localhost. After connecting, you should see:
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/
Implicit session: session { "id" : UUID("70ba1cae-7fd6-4c56-a77f-6b713c674ee6") }
MongoDB server version: 4.2.8
......
>
Let‘s create our first database and collection!
Creating a Database and Collection
MongoDB stores data in databases which contains collections of documents. Think of it as folders containing files.
A single MongoDB server can host multiple independent databases.
To create a new database, use the use command:
use mytestdb
This will switch the shell context to mytestdb database. To create a collection named ‘products‘ in this database:
db.createCollection(‘products‘)
That collection is now ready to store documents!
For reference, some other useful database commands are:
# List databases
show dbs
# Drop a database
db.dropDatabase()
# List collections
show collections
# Drop a collection
db.products.drop()
As you can see, MongoDB provides handy CLI to manage databases and collections.
Now let‘s insert and query documents!
Inserting Documents in MongoDB
Documents in MongoDB are just BSON objects – JSON-style key-value pairs.
To insert a document into a collection, we can use the insertOne() method:
db.products.insertOne({
name: ‘Apple iPhone 12‘,
brand: ‘Apple‘,
price: 999,
category: ‘smartphones‘
})
This inserts a new document into the products collection.
To insert multiple documents together, we can pass an array of objects to insertMany():
db.products.insertMany([
{
name: ‘OnePlus 9 Pro‘,
brand: ‘OnePlus‘,
price: 799
},
{
name: ‘Samsung S21 Ultra‘,
brand: ‘Samsung‘,
price: 1299
}
])
That‘s it! Our products collection now contains some initial seed data to query and manipulate.
Querying Documents in MongoDB
MongoDB provides a rich set of operators to filter documents from a collection.
To fetch all documents in a collection, we can use the find() method:
db.products.find()
This will return a cursor with all documents.
We can filter by passing a query document to find():
db.products.find({
brand: ‘Samsung‘
})
This will return all documents where brand is ‘Samsung‘.
To return a subset of fields from matching documents, we can pass a projection document:
db.products.find(
{brand: ‘Samsung‘},
{name: 1, price: 1}
)
This returns only name and price fields.
There are a ton of query operators like $gt, $regex, $in etc to support advanced finding of documents. Let‘s look at some examples:
Equality:
db.products.find({price: 1299})
Greater Than:
db.products.find({price: {$gt: 1000}})
In Clause:
db.products.find({category: {$in: [‘smartphones‘, ‘laptops‘]}})
Regular Expressions:
db.products.find({name: {$regex: ‘pro‘}})
This is just a small sample of the powerful querying engine available!
Updating Documents in MongoDB
To make changes to documents, MongoDB provides update operators like updateOne() and updateMany().
updateOne() updates the first matching document:
db.products.updateOne(
{_id: 1},
{$set: {price: 859}}
)
updateMany() updates all matching documents:
db.products.updateMany(
{brand: ‘Apple‘},
{$set: {category: ‘smartphone‘}}
)
This will add the category field to all Apple products.
There are specialized update operators like:
- $inc to increment a field
- $push to append to an array
- $pull to remove from an array
And UPSERT updates if a document doesn‘t exist.
Deleting Documents in MongoDB
To remove documents, we can use deleteOne() and deleteMany() methods.
deleteOne() removes the first matching document:
db.products.deleteOne({name: ‘Apple iPhone 12‘})
deleteMany() removes all matching documents:
db.products.deleteMany({price: {$lt: 1000}})
This deletes all products cheaper than 1000.
That covers the basics of CRUD operations in MongoDB. But there‘s a lot more power like indexing, aggregation and transactions. Let‘s look at some highlights.
Indexing for Faster Queries
Indexes make queries much faster by avoiding collection scans. To create an index on a field, we can run:
db.products.createIndex({brand: 1})
This will index the brand field in ascending order.
We can also create compound indexes on multiple fields:
db.products.createIndex({brand: 1, category: 1})
MongoDB supports secondary indexes, partial indexes, unique constraints and more.
With indexes in place, queries will show improved performance.
Aggregation for Data Analysis
The aggregation framework allows complex analytical and data processing tasks on MongoDB collections. Some useful operators are:
$match – Filter documents
{$match: {category: "smartphones"}}
$group – Group documents by a field
{$group: {_id: "$brand", totalSales: {$sum: "$price"}}}
$sort – Sorts documents
{$sort: {totalSales: -1}}
$lookup– Joins collections
{$lookup: {from: "orders", localField: "name", foreignField: "product", as: "orders"}}
There are many more stages like $project, $graphLookup, $geoNear and different ways to reshape, analyze and process data using aggregations.
Transactions for ACID Compliance
MongoDB 4.0 added support for multi-document ACID transactions so you can handle complex workflows confidently.
To run operations in a transaction, we define the callback:
const session = client.startSession();
const transactionOptions = {
readPreference: ‘primary‘,
readConcern: { level: ‘local‘ },
writeConcern: { w: ‘majority‘ }
};
const transactionCallback = async () => {
const collection = session.getDatabase(...).getCollection(....);
// Run transaction operations...
const result = await collection.insertOne(...);
return result;
}
session.withTransaction(transactionCallback, transactionOptions);
This provides atomicity, consistency and isolation while updating multiple documents.
Transactions open up new patterns for building robust applications.
Well, that was a quick tour of MongoDB capabilities! Let‘s now look at managing databases visually using MongoDB Compass.
Using MongoDB Compass GUI
So far we used the mongo shell to interact with MongoDB. For a more visual approach, MongoDB offers the Compass GUI tool.
Compass allows managing databases with an intuitive interface:
With Compass, you can:
- Connect to local or remote MongoDB servers
- View and browse database and collections
- Insert, modify, delete documents visually
- Construct queries and view results with filters
- Perform aggregations and analyze using charts
- View database stats and document validation
- Manage indexes, storage and more
I highly recommend using Compass alongside the shell to get a complete MongoDB experience!
It‘s available for download on mongodb.com/products/compass.
Next, let‘s connect MongoDB to our application code.
Connecting from Code
To use MongoDB in your applications, MongoDB provides officially supported drivers for languages like:
- Node.js
- Python
- Java
- C#/.NET
- C++
- Go
- PHP
- Ruby
These drivers handle connection management, serialization and integrate well with language-specific coding styles.
Let‘s see a simple example of connecting Node.js to MongoDB:
// Import MongoDB driver
const MongoClient = require(‘mongodb‘).MongoClient;
// Connection URL
const url = ‘mongodb://localhost:27017‘;
// Database Name
const dbName = ‘myproject‘;
// Create client
const client = new MongoClient(url);
// Connect
client.connect(function(err) {
if (err) throw err;
console.log("Connected successfully to server");
const db = client.db(dbName);
// Perform database operations...
client.close();
});
That gives a quick sample of how drivers are used to connect and work with MongoDB programmatically.
For other languages, the concepts are similar with language-specific APIs.
Summary
We‘ve covered a LOT of ground in this comprehensive beginner‘s guide!
Let‘s recap some key learnings:
- MongoDB is a popular document database that stores flexible JSON-like data
- Easy to install and run MongoDB server on macOS, Windows and Linux
- Interact with MongoDB via the mongo shell and Compass GUI
- Databases contain collections which hold BSON documents
- Documents can be inserted, queried, updated and deleted
- Powerful indexing and aggregation capabilities
- Transactions provide ACID compliance
- MongoDB drivers available for many languages
This guide focused on core concepts, but MongoDB has many more capabilities we couldn‘t cover like:
- Config servers and sharding
- Replication and high availability
- GridFS for large files
- MongoDB Atlas cloud database
- MongoDB Realm for mobile and web development
- MongoDB Charts for data visualization
I hope you enjoyed this introduction and are pumped up to start using MongoDB for your projects! To take a structured course, I recommend:
- MongoDB University for free online courses
- MongoDB Certification to validate your skills
Finally, if you need any help or have questions, feel free to reach me on Twitter @mkennedy.
Now go crush it with MongoDB!