Getting yourself setup with a Free MONGO DB Atlas in the Cloud Account:
1. Introduction to MongoDB
2. Installing MongoDB on Different Operating Systems
- Windows
- macOS
- Linux
3. Setting Up MongoDB Atlas
4. Connecting to MongoDB Using MongoDB Compass
5. Basic Commands to Verify Installation
6. Troubleshooting Common Issues
---
### 1. Introduction to MongoDB
**MongoDB** is a NoSQL database that provides high performance, high availability, and easy scalability. It uses a document-oriented data model and supports dynamic schemas, making it an excellent choice for storing JSON-like documents.
### 2. Installing MongoDB on Different Operating Systems
#### Installing MongoDB on Windows
1. **Download MongoDB Installer**
- Visit the [MongoDB Download Center](https://www.mongodb.com/try/download/community) and select the version for Windows.
- Download the `.msi` installer.
2. **Run the Installer**
- Double-click the downloaded `.msi` file.
- Follow the installation prompts. Choose "Complete" setup.
3. **Set Up the Environment**
- After installation, add the MongoDB binary directory (`C:\Program Files\MongoDB\Server\<version>\bin`) to your system's `PATH` environment variable.
4. **Create Data Directory**
- MongoDB requires a data directory to store its data. By default, it will use `C:\data\db`. Create this directory using:
```cmd
md \data\db
```
5. **Run MongoDB**
- Open a command prompt and run:
```cmd
mongod
```
- This will start the MongoDB server.
6. **Verify Installation**
- Open another command prompt and run:
```cmd
mongo
```
- This will start the MongoDB shell.
#### Installing MongoDB on macOS
1. **Install Homebrew**
- Homebrew is a package manager for macOS. If you haven't installed it yet, you can do so by running:
```sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
2. **Install MongoDB**
- Run the following command to install MongoDB:
```sh
brew tap mongodb/brew
brew install mongodb-community@5.0
```
3. **Start MongoDB**
- Start MongoDB using:
```sh
brew services start mongodb/brew/mongodb-community
```
4. **Verify Installation**
- Open a terminal and run:
```sh
mongo
```
- This will start the MongoDB shell.
#### Installing MongoDB on Linux (Ubuntu)
1. **Import the Public Key**
- Import the MongoDB public GPG key:
```sh
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
```
2. **Create the List File**
- Create a list file for MongoDB:
```sh
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
```
3. **Reload the Local Package Database**
- Run:
```sh
sudo apt-get update
```
4. **Install MongoDB Packages**
- Install the MongoDB packages:
```sh
sudo apt-get install -y mongodb-org
```
5. **Start MongoDB**
- Start the MongoDB service:
```sh
sudo systemctl start mongod
```
6. **Verify Installation**
- Run:
```sh
mongo
```
- This will start the MongoDB shell.
### 3. Setting Up MongoDB Atlas
MongoDB Atlas is a cloud-based database service that provides an easy way to set up and manage MongoDB clusters.
1. **Sign Up for MongoDB Atlas**
- Visit [MongoDB Atlas](https://www.mongodb.com/cloud/atlas/register) and sign up for a free account.
2. **Create a New Cluster**
- Follow the prompts to create a new cluster. Choose the free tier for this exercise.
3. **Set Up Your Cluster**
- After the cluster is created, click "Connect" and follow the instructions to whitelist your IP address and create a database user.
4. **Connect to Your Cluster**
- You will be provided with a connection string. It looks something like this:
```
mongodb+srv://<username>:<password>@cluster0.mongodb.net/test
```
- Replace `<username>` and `<password>` with the credentials you created.
### 4. Connecting to MongoDB Using MongoDB Compass
MongoDB Compass is a graphical user interface that allows you to interact with your MongoDB data.
1. **Download MongoDB Compass**
- Visit the [MongoDB Compass Download Page](https://www.mongodb.com/products/compass) and download the appropriate version for your operating system.
2. **Install MongoDB Compass**
- Follow the installation instructions for your operating system.
3. **Connect to Your MongoDB Instance**
- Open MongoDB Compass and enter the connection string you obtained from MongoDB Atlas.
- Click "Connect" to establish the connection.
### 5. Basic Commands to Verify Installation
Once connected to MongoDB (either through the shell or MongoDB Compass), you can run a few basic commands to verify the installation.
#### MongoDB Shell
1. **Show Databases**
```sh
show dbs
```
2. **Create a Database**
```sh
use myTestDB
```
3. **Insert a Document**
```sh
db.myCollection.insertOne({ name: "John Doe", age: 25 })
```
4. **Find Documents**
```sh
db.myCollection.find()
```
#### MongoDB Compass
1. **Create a Database**
- Click on "Create Database", name it `myTestDB`, and add a collection `myCollection`.
2. **Insert a Document**
- Click on `myCollection`, and then click on "Insert Document". Add:
```json
{
"name": "John Doe",
"age": 25
}
```
3. **View Documents**
- Click on "Find" to see the inserted documents.
### 6. Troubleshooting Common Issues
1. **Connection Issues**
- Ensure MongoDB server is running (`mongod`).
- Check network settings and whitelist IP addresses in MongoDB Atlas.
2. **Permission Issues**
- Ensure you have the correct permissions for the directories MongoDB uses.
3. **Environment Variable Issues**
- Ensure MongoDB bin directory is added to the `PATH` environment variable.
Conclusion
By following these steps, you will have installed and set up MongoDB on your system, created a database, and performed basic CRUD operations. This foundational knowledge will help you as you continue to work with MongoDB in more advanced scenarios.