
brew services start mongodb/brew/mongodb-community
Verify the installation: Check if MongoDB is running:
bash
Copyps aux | grep mongod
Stop MongoDB: To stop MongoDB, use:
bash
Copybrew services stop mongodb/brew/mongodb-community
For Windows
Download MongoDB: Go to the MongoDB Download Center and download the installer for your version of Windows.
Run the Installer: Once the installer is downloaded, run it. Choose the Complete installation type. You can also choose to install MongoDB as a service, which will automatically start MongoDB when your system starts.
Configure MongoDB (Optional): You can configure the directory where MongoDB stores its data. By default, it’s stored in C:\data\db. If you want to change the location, you can modify it in the mongod.cfg configuration file.
Start MongoDB: After installation, MongoDB will start automatically if you opted to install it as a service. You can manually start it using the command:
Copymongod --dbpath C:\data\db
Access MongoDB: To connect to the MongoDB server, open another command prompt window and type:
mongo
brew services start mongodb/brew/mongodb-community
Verify the installation: Check if MongoDB is running:
bash
Copyps aux | grep mongod
Stop MongoDB: To stop MongoDB, use:
bash
Copybrew services stop mongodb/brew/mongodb-community
For Windows
Download MongoDB: Go to the MongoDB Download Center and download the installer for your version of Windows.
Run the Installer: Once the installer is downloaded, run it. Choose the Complete installation type. You can also choose to install MongoDB as a service, which will automatically start MongoDB when your system starts.
Configure MongoDB (Optional): You can configure the directory where MongoDB stores its data. By default, it’s stored in C:\data\db. If you want to change the location, you can modify it in the mongod.cfg configuration file.
Start MongoDB: After installation, MongoDB will start automatically if you opted to install it as a service. You can manually start it using the command:
Copymongod --dbpath C:\data\db
Access MongoDB: To connect to the MongoDB server, open another command prompt window and type:
mongo
Download MongoDB Shell:
Go to the MongoDB Download Center and download the latest version of MongoDB Shell for Windows (available as a .zip archive).
Extract the Downloaded Files: Extract the downloaded .zip archive to a directory, such as C:\mongodb-shell.
Add MongoDB Shell to PATH (Optional):
Add the directory where MongoDB Shell is extracted to the system’s PATH variable so that you can run it from any command prompt:
Right-click This PC > Properties > Advanced system settings > Environment Variables.
Under System Variables, find and select Path, then click Edit.
Add the path to the MongoDB Shell directory (e.g., C:\mongodb-shell).
Verify Installation: Open the command prompt and check the mongo shell version:
mongo --version
1. Start MongoDB Shell
First, make sure MongoDB is running and then open the MongoDB shell by typing the following command in your terminal:
mongo
If MongoDB is running locally on the default port, it should automatically connect to the test database. If you’re connecting to a remote server, you can use this command:
mongo <hostname>:<port>
2. Create a Database
To create a new database, use the use command. MongoDB does not actually create the database until you insert some data into it. Here’s how you create and switch to a new database:
javascript
use myNewDatabase
This switches to the myNewDatabase database. If it doesn’t already exist, MongoDB will create it once you insert some data into it.
3. Create a Collection
Collections are automatically created when you insert documents into them. You can also explicitly create a collection using the db.createCollection() method.
Method 1: Insert a Document (Implicitly Creates Collection)
To create a collection, simply insert a document into the collection. Here’s an example:
javascript
db.myCollection.insertOne({
name: "John",
age: 30,
city: "New York"
})
insertOne(): Inserts a single document into a collection.
insertMany(): Inserts multiple documents into a collection.
Let's say we want to update the age of a user named "Alice" to 26:
javascript
db.myCollection.updateOne(
{ name: "Alice" }, // filter: find the document where name is Alice
{ $set: { age: 26 } } // update: set the age field to 26
)
Explanation:
{ name: "Alice" }: This is the filter, which selects the document to be updated.
{ $set: { age: 26 } }: The $set operator is used to update the value of the age field to 26.
2. Update Multiple Documents
If you want to update multiple documents, you can use the updateMany() method. This updates all documents that match the filter criteria.
Syntax:
javascript
db.collection.updateMany(
{ <filter> },
{ <update> },
{ <options> }
)
Example:
Let's say you want to update the city field to "San Francisco" for all users who are older than 30:
javascript
db.myCollection.updateMany(
{ age: { $gt: 30 } }, // filter: find documents where age is greater than 30
{ $set: { city: "San Francisco" } } // update: set city to San Francisco
)
Explanation:
{ age: { $gt: 30 } }: This filter finds all documents where the age is greater than 30.
{ $set: { city: "San Francisco" } }: This updates the city field for all matched documents.
Welcome to the MongoDB Beginner Course! This course is designed to introduce you to the basics of MongoDB, one of the most popular NoSQL databases used by developers worldwide. Whether you are new to databases or transitioning from relational database systems like MySQL or PostgreSQL, this course will help you understand MongoDB's unique features and how to use it effectively.
Course Overview:
1. What is MongoDB?
Introduction to NoSQL Databases: Learn about the NoSQL paradigm and why MongoDB is used as an alternative to traditional relational databases.
Document-Oriented Database: Understand how MongoDB stores data in flexible, JSON-like documents rather than rows and tables.
2. Key Concepts in MongoDB:
Collections & Documents: Dive into the structure of MongoDB. Collections hold documents, which contain data in key-value pairs.
CRUD Operations: Learn how to Create, Read, Update, and Delete documents in a MongoDB database.
Indexes: Understand how indexes improve query performance in MongoDB.
Aggregation: Learn how to perform complex queries, group, filter, and transform data using the aggregation framework.
3. MongoDB Setup & Installation:
Learn how to install MongoDB on your local machine or set up a cloud-based MongoDB instance.
Introduction to MongoDB Atlas (cloud service) for remote database hosting.
4. Basic MongoDB Operations:
Creating a Database: Learn to create and switch between databases in MongoDB.
Inserting Data: Understand how to insert single or multiple documents into collections.
Querying Data: Learn how to retrieve data with basic find queries, filters, and conditions.
Updating & Deleting Data: Learn how to update and delete documents using simple commands.
Working with Data Types: Understand different data types like strings, numbers, arrays, and embedded documents.