Assignments for Day 2
Installing MongoDB
Assignment 1: Install MongoDB on Windows
Task: Follow the steps to install MongoDB on your Windows machine. Ensure MongoDB is correctly installed and running.
Solution:
1. Download the MongoDB .msi installer from the MongoDB Download Center.
2. Run the installer and select the Complete option.
3. Choose the option to install MongoDB as a service.
4. Add MongoDB’s bin directory to the system’s PATH.
5. Open the Command Prompt and run mongod to start MongoDB.
6. In a new Command Prompt window, type mongo to connect to the MongoDB shell.
If you successfully connect to the MongoDB shell, MongoDB is installed and running.
Assignment 2: Install MongoDB on macOS using Homebrew
Task: Install MongoDB on your macOS machine using the Homebrew package manager. Start and verify the MongoDB service.
Solution:
1. Install Homebrew (if not installed) by running:
bash
CopyEdit
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Install MongoDB using Homebrew:
bash
CopyEdit
brew tap mongodb/brew
brew install mongodb-community@5.0
3. Start the MongoDB service:
bash
CopyEdit
brew services start mongodb/brew/mongodb-community
4. To verify, run the mongo command to enter the MongoDB shell.
Assignment 3: Install MongoDB on Ubuntu (Linux)
Task: Install MongoDB on your Ubuntu Linux system and confirm that MongoDB is running.
Solution:
1. Update the package list:
bash
CopyEdit
sudo apt update
2. Install the necessary dependencies:
bash
CopyEdit
sudo apt install -y gnupg
3. Add the MongoDB repository:
bash
CopyEdit
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
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
4. Install MongoDB:
bash
CopyEdit
sudo apt update
sudo apt install -y mongodb-org
5. Start MongoDB:
bash
CopyEdit
sudo systemctl start mongod
6. Verify by running:
bash
CopyEdit
mongo
Assignment 4: Verify MongoDB Service Status
Task: Check the status of the MongoDB service on your system to verify whether it is running correctly.
Solution:
1. On Windows:
o Open Task Manager and search for mongod.exe to verify the process is running.
2. On macOS and Linux:
o Run the following command:
bash
CopyEdit
sudo systemctl status mongod
o If MongoDB is running, the output should show active (running).
Assignment 5: Modify MongoDB Configuration File
Task: Modify MongoDB’s configuration file to change the bind IP and port number.
Solution:
1. Locate the MongoDB configuration file:
o On Linux: /etc/mongod.conf
o On macOS: /usr/local/etc/mongod.conf
o On Windows: C:\Program Files\MongoDB\Server\5.0\bin\mongod.cfg
2. Open the configuration file in a text editor and locate the net section.
3. Change the bindIp to 0.0.0.0 and the port to 27018 (for example):
yaml
CopyEdit
net:
port: 27018
bindIp: 0.0.0.0
4. Save the file and restart MongoDB:
bash
CopyEdit
sudo systemctl restart mongod
5. Verify that the changes have taken effect by running mongo and checking the port.
Assignment 6: Start and Stop MongoDB on macOS
Task: Start and stop MongoDB on macOS using the brew service commands.
Solution:
1. To start MongoDB:
bash
CopyEdit
brew services start mongodb/brew/mongodb-community
2. To stop MongoDB:
bash
CopyEdit
brew services stop mongodb/brew/mongodb-community
3. To check the status:
bash
CopyEdit
brew services list
Assignment 7: Run MongoDB as a Service on Linux
Task: Ensure MongoDB starts automatically when your system boots on Linux.
Solution:
1. Run the following command to enable MongoDB to start on boot:
bash
CopyEdit
sudo systemctl enable mongod
2. Restart the system and check the status of MongoDB:
bash
CopyEdit
sudo systemctl status mongod
o The output should show active (running).
Assignment 8: Connect to MongoDB Using Mongo Shell
Task: Once MongoDB is installed, connect to it using the Mongo shell and display information about the system.
Solution:
1. Open the terminal/command prompt and type:
bash
CopyEdit
mongo
2. Once connected, run the following command to display MongoDB server information:
bash
CopyEdit
db.serverStatus()
3. This will return server details like memory usage, uptime, and other configuration information.
Assignment 9: Create a Database and Collection in MongoDB
Task: Create a new database and collection in MongoDB and insert a sample document.
Solution:
1. Connect to MongoDB using the mongo shell.
2. Create a new database:
bash
CopyEdit
use myDatabase
3. Create a new collection and insert a document:
bash
CopyEdit
db.myCollection.insertOne({name: "John Doe", age: 30, profession: "Software Developer"})
4. Verify the insertion:
bash
CopyEdit
db.myCollection.find()
Assignment 10: Check MongoDB Log Files
Task: Locate the MongoDB log files and inspect them for any errors or warnings.
Solution:
1. On Linux and macOS, the log file is typically located at /var/log/mongodb/mongod.log (for Linux) and /usr/local/var/log/mongodb/mongo.log (for macOS).
2. To view the MongoDB log file, run:
bash
CopyEdit
cat /var/log/mongodb/mongod.log
or use tail -f to view logs in real-time:
bash
CopyEdit
tail -f /var/log/mongodb/mongod.log
3. Look for any errors or warnings that may indicate issues with MongoDB.
Conclusion
By completing these assignments, you should now be comfortable with installing MongoDB on various platforms, configuring MongoDB, starting and stopping services, and performing basic MongoDB operations.
