MongoDB Step-by-Step Tutorial for Beginners
MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications.
MongoDB Step-by-Step Tutorial for Beginners
MongoDB Step-by-Step Tutorial for Beginners
MongoDB Step-by-Step Tutorial for Beginners
MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications. This tutorial will guide you through the basics, from installation to common operations. We'll assume you're using a local setup for learning purposes. For production, consider MongoDB Atlas (cloud-hosted service).
Prerequisites: Basic command-line knowledge and a supported OS (Windows, macOS, or Linux).
Step 1: Installation
MongoDB Community Edition is free and available for major operating systems. Download from the official website: https://www.mongodb.com/try/download/community.
On Windows:
- Download the MSI installer for your Windows version.
- Run the installer and follow the prompts. Select "Complete" setup and install as a service (recommended for easy startup).
- Add the MongoDB bin directory (e.g.,
C:\Program Files\MongoDB\Server\7.0\bin) to your system's PATH environment variable. - Verify installation by opening Command Prompt and running
mongod --version.
On macOS:
- Use Homebrew (install if not present:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"). - Run
brew tap mongodb/brewto add the MongoDB tap. - Install with
brew install mongodb-community@7.0. - Start the service:
brew services start mongodb/brew/mongodb-community. - Verify:
mongod --version.
On Ubuntu (Linux example):
- Import the public key:
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -. - Create a list file:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list. - Update packages:
sudo apt-get update. - Install:
sudo apt-get install -y mongodb-org. - Start the service:
sudo systemctl start mongod. - Verify:
mongod --versionand check status withsudo systemctl status mongod.
Note: Version 7.0 is current as of 2025; check for updates. If issues arise, refer to official docs.
Step 2: Starting the MongoDB Server
- Open a terminal/command prompt.
- Run
mongod(or if installed as service, it's already running). - The server listens on port 27017 by default. You'll see logs indicating it's ready.
Step 3: Connecting to MongoDB (Using Mongo Shell)
- Open another terminal.
- Run
mongosh(the modern shell; older versions usemongo). - You'll enter the MongoDB shell prompt:
test>(default database is 'test'). - To exit:
exit.
Step 4: Creating a Database
Databases are created implicitly when you use them.
1. In the shell, switch to a new database: use myDatabase.
2. MongoDB creates it when you insert data.
- Show databases: show dbs.
- Current database: db.
Step 5: Creating Collections and Inserting Documents (Create in CRUD)
Collections are like tables and are also created implicitly.
1. Insert a document: db.myCollection.insertOne({ name: "John Doe", age: 30, city: "New York" }).
- This creates 'myCollection' if it doesn't exist.
2. Insert multiple: db.myCollection.insertMany([{ name: "Jane Smith", age: 25 }, { name: "Bob Johnson", age: 35 }]).
- Documents are BSON objects with key-value pairs.
Step 6: Querying Data (Read in CRUD)
- Find all documents:
db.myCollection.find(). - Pretty print:
db.myCollection.find().pretty(). - Filter:
db.myCollection.find({ age: { $gt: 25 } })(age greater than 25). - Limit results:
db.myCollection.find().limit(2). - Sort:
db.myCollection.find().sort({ age: 1 })(ascending by age). - Use operators like
$eq,$ne,$gt,$lt,$in, etc.
Step 7: Updating Data (Update in CRUD)
- Update one:
db.myCollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } }). - Update many:
db.myCollection.updateMany({ age: { $lt: 30 } }, { $inc: { age: 1 } })(increment age by 1). - Replace:
db.myCollection.replaceOne({ name: "John Doe" }, { name: "John Doe", age: 32, city: "Boston" }).
Step 8: Deleting Data (Delete in CRUD)
- Delete one:
db.myCollection.deleteOne({ name: "Bob Johnson" }). - Delete many:
db.myCollection.deleteMany({ age: { $gt: 30 } }). - Drop collection:
db.myCollection.drop(). - Drop database:
db.dropDatabase().
Step 9: Indexing
Indexes speed up queries.
1. Create index: db.myCollection.createIndex({ age: 1 }) (ascending on age).
2. Unique index: db.myCollection.createIndex({ name: 1 }, { unique: true }).
3. View indexes: db.myCollection.getIndexes().
Step 10: Aggregation
For complex data processing.
1. Basic pipeline: db.myCollection.aggregate([{ $match: { age: { $gt: 25 } } }, { $group: { _id: "$city", count: { $sum: 1 } } }]).
- Matches documents, groups by city, counts them.
Additional Topics
- Tools: Use MongoDB Compass (GUI) for visual management.
- Drivers: For app integration, use official drivers (e.g., Node.js:
npm install mongodb). - Security: Enable authentication in production; create users with
db.createUser(). - Replication/Sharding: For high availability and scaling, set up replica sets or shards.
Practice these commands in your shell. For cloud setup, try MongoDB Atlas free tier. If you're using a programming language, refer to language-specific docs (e.g., PyMongo for Python).
This covers the fundamentals. For advanced topics, explore the official documentation or courses.
MongoDB Step-by-Step Tutorial for Beginners
MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications.
MongoDB Step-by-Step Tutorial for Beginners
MongoDB Step-by-Step Tutorial for Beginners
MongoDB Step-by-Step Tutorial for Beginners
MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications. This tutorial will guide you through the basics, from installation to common operations. We'll assume you're using a local setup for learning purposes. For production, consider MongoDB Atlas (cloud-hosted service).
Prerequisites: Basic command-line knowledge and a supported OS (Windows, macOS, or Linux).
Step 1: Installation
MongoDB Community Edition is free and available for major operating systems. Download from the official website: https://www.mongodb.com/try/download/community.
On Windows:
- Download the MSI installer for your Windows version.
- Run the installer and follow the prompts. Select "Complete" setup and install as a service (recommended for easy startup).
- Add the MongoDB bin directory (e.g.,
C:\Program Files\MongoDB\Server\7.0\bin) to your system's PATH environment variable. - Verify installation by opening Command Prompt and running
mongod --version.
On macOS:
- Use Homebrew (install if not present:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"). - Run
brew tap mongodb/brewto add the MongoDB tap. - Install with
brew install mongodb-community@7.0. - Start the service:
brew services start mongodb/brew/mongodb-community. - Verify:
mongod --version.
On Ubuntu (Linux example):
- Import the public key:
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -. - Create a list file:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list. - Update packages:
sudo apt-get update. - Install:
sudo apt-get install -y mongodb-org. - Start the service:
sudo systemctl start mongod. - Verify:
mongod --versionand check status withsudo systemctl status mongod.
Note: Version 7.0 is current as of 2025; check for updates. If issues arise, refer to official docs.
Step 2: Starting the MongoDB Server
- Open a terminal/command prompt.
- Run
mongod(or if installed as service, it's already running). - The server listens on port 27017 by default. You'll see logs indicating it's ready.
Step 3: Connecting to MongoDB (Using Mongo Shell)
- Open another terminal.
- Run
mongosh(the modern shell; older versions usemongo). - You'll enter the MongoDB shell prompt:
test>(default database is 'test'). - To exit:
exit.
Step 4: Creating a Database
Databases are created implicitly when you use them.
1. In the shell, switch to a new database: use myDatabase.
2. MongoDB creates it when you insert data.
- Show databases: show dbs.
- Current database: db.
Step 5: Creating Collections and Inserting Documents (Create in CRUD)
Collections are like tables and are also created implicitly.
1. Insert a document: db.myCollection.insertOne({ name: "John Doe", age: 30, city: "New York" }).
- This creates 'myCollection' if it doesn't exist.
2. Insert multiple: db.myCollection.insertMany([{ name: "Jane Smith", age: 25 }, { name: "Bob Johnson", age: 35 }]).
- Documents are BSON objects with key-value pairs.
Step 6: Querying Data (Read in CRUD)
- Find all documents:
db.myCollection.find(). - Pretty print:
db.myCollection.find().pretty(). - Filter:
db.myCollection.find({ age: { $gt: 25 } })(age greater than 25). - Limit results:
db.myCollection.find().limit(2). - Sort:
db.myCollection.find().sort({ age: 1 })(ascending by age). - Use operators like
$eq,$ne,$gt,$lt,$in, etc.
Step 7: Updating Data (Update in CRUD)
- Update one:
db.myCollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } }). - Update many:
db.myCollection.updateMany({ age: { $lt: 30 } }, { $inc: { age: 1 } })(increment age by 1). - Replace:
db.myCollection.replaceOne({ name: "John Doe" }, { name: "John Doe", age: 32, city: "Boston" }).
Step 8: Deleting Data (Delete in CRUD)
- Delete one:
db.myCollection.deleteOne({ name: "Bob Johnson" }). - Delete many:
db.myCollection.deleteMany({ age: { $gt: 30 } }). - Drop collection:
db.myCollection.drop(). - Drop database:
db.dropDatabase().
Step 9: Indexing
Indexes speed up queries.
1. Create index: db.myCollection.createIndex({ age: 1 }) (ascending on age).
2. Unique index: db.myCollection.createIndex({ name: 1 }, { unique: true }).
3. View indexes: db.myCollection.getIndexes().
Step 10: Aggregation
For complex data processing.
1. Basic pipeline: db.myCollection.aggregate([{ $match: { age: { $gt: 25 } } }, { $group: { _id: "$city", count: { $sum: 1 } } }]).
- Matches documents, groups by city, counts them.
Additional Topics
- Tools: Use MongoDB Compass (GUI) for visual management.
- Drivers: For app integration, use official drivers (e.g., Node.js:
npm install mongodb). - Security: Enable authentication in production; create users with
db.createUser(). - Replication/Sharding: For high availability and scaling, set up replica sets or shards.
Practice these commands in your shell. For cloud setup, try MongoDB Atlas free tier. If you're using a programming language, refer to language-specific docs (e.g., PyMongo for Python).
This covers the fundamentals. For advanced topics, explore the official documentation or courses.