Lab Manual - Mongodb

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Lab Manual: MongoDB Installation

Objectives

 Install MongoDB on your machine.


 Set up the environment.
 Verify the installation.

Prerequisites

 Basic knowledge of command line usage.


 Administrative privileges on your computer.

Installation Steps

Step 1: Download MongoDB

1. Visit the MongoDB Download Center: Go to MongoDB Community Server.


2. Select Your Version: Choose your operating system (Windows, macOS, or Linux).
3. Download the Installer: Click the download link for the version you want.

Step 2: Install MongoDB

For Windows:

1. Run the downloaded installer (.msi file).


2. Follow the installation wizard:
o Choose "Complete" installation.
o Check the option to install MongoDB as a Service (recommended).
3. Click "Finish" once the installation completes.

For macOS:

1. If you have Homebrew installed, run:

bash
brew tap mongodb/brew

brew install mongodb-community

2. Otherwise, you can download the .tgz file and follow the instructions to extract and set it up
manually.
For Linux (Ubuntu example):
Open the terminal.
Run the following commands:

wget -qO - https://www.mongodb.org/static/pgp/server-<version>.asc | sudo apt-key add -


echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/multiverse amd64
packages" | sudo tee /etc/apt/sources.list.d/mongodb-org-<version>.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Step 3: Create the Data Directory

MongoDB stores its data in /data/db by default. Create this directory and set permissions:

# For macOS/Linux
sudo mkdir -p /data/db
sudo chown `id -u` /data/db

For Windows, you may skip this step as MongoDB will create the necessary directories
automatically.

Step 4: Start MongoDB

 Windows: Start the MongoDB service using the command prompt:

net start MongoDB


macOS/Linux: Start MongoDB with:
mongod

Step 5: Verify the Installation

1. Open another terminal or command prompt.


2. Start the MongoDB shell by typing:

Mongosh

1. You should see the MongoDB shell prompt (>) if it starts correctly.

Step 6: Basic MongoDB Commands

Once in the MongoDB shell, you can try the following commands:

1. Create a new database:

use myTestDB
2. Create a new collection and insert a document:
db.myCollection.insertOne({ name: "Alice", age: 25 })
3. View documents:
db.myCollection.find().pretty()
Conclusion

You have successfully installed MongoDB and performed basic operations! Explore more
features and commands as you continue your MongoDB journey.

Troubleshooting Tips

 Ensure MongoDB is running before accessing it through the shell.


 Check your firewall settings if you have connection issues.

You might also like