0% found this document useful (0 votes)
6 views8 pages

MongoDB Setup and Usage Guide

MongoDB is a NoSQL, document-oriented database that stores data in BSON documents, ideal for unstructured data and flexible schemas. It can be set up locally or through MongoDB Atlas, a cloud-based service, and is commonly used in applications requiring real-time analytics and content management. CRUD operations can be performed using Express and Mongoose to interact with the database in Node.js applications.

Uploaded by

ayush05deo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

MongoDB Setup and Usage Guide

MongoDB is a NoSQL, document-oriented database that stores data in BSON documents, ideal for unstructured data and flexible schemas. It can be set up locally or through MongoDB Atlas, a cloud-based service, and is commonly used in applications requiring real-time analytics and content management. CRUD operations can be performed using Express and Mongoose to interact with the database in Node.js applications.

Uploaded by

ayush05deo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-6 FULL STACK AMT

1. What is MongoDB?
Definition:
MongoDB is a NoSQL, open-source, document-oriented database designed for
high performance, high availability, and easy scalability.
Instead of storing data in rows and columns (tables) like relational databases,
MongoDB stores data in flexible, JSON-like documents called BSON (Binary
JSON).

Key Features of MongoDB:

How MongoDB Stores Data:


• Database → Contains collections
• Collection → Group of related documents (similar to tables)
• Document → JSON-like data structure with key-value pairs
Example Document:
{
"name": "Aditya",
"age": 21,
"skills": ["JavaScript", "React", "[Link]"]
}

Differences: MongoDB vs SQL Database

Example: SQL vs MongoDB Data


SQL:
SELECT * FROM users WHERE age > 20;
MongoDB:
[Link]({ age: { $gt: 20 } })

Use Cases of MongoDB:


• Real-time analytics
• Content management systems
• E-commerce product catalogs
• IoT and mobile apps
• Applications with rapidly changing or unstructured data
Tools Used with MongoDB:

Summary:
• MongoDB is a document-based NoSQL database ideal for handling
unstructured or semi-structured data.
• It uses flexible schemas, making it easy to evolve data models over time.
• Data is stored in BSON documents and grouped in collections.
• It is commonly used with [Link], especially in the MERN stack.

2. Setting Up MongoDB
MongoDB can be set up in two main ways:
1. Local Installation – Installing MongoDB on your computer
2. Cloud-Based (MongoDB Atlas) – Using MongoDB’s managed cloud
database service
Option 1: Local Installation of MongoDB
Step 1: Download MongoDB
• Visit: [Link]
• Choose your OS (Windows/macOS/Linux)
• Select "Current Release" and install the MongoDB Community Edition

Step 2: Install MongoDB


• Run the downloaded installer
• Select "Complete Setup"
• Ensure MongoDB Server and MongoDB Compass (optional GUI) are
selected
MongoDB is usually installed at:
• C:\Program Files\MongoDB\Server\6.0\bin (Windows)

Step 3: Add MongoDB to PATH (Windows only)


• Go to: Environment Variables → System Variables → Path
• Add this:
C:\Program Files\MongoDB\Server\6.0\bin
Now, open a terminal (Command Prompt or PowerShell) and type:
mongod # Starts the MongoDB server
mongo # Opens the Mongo Shell (older versions)

Step 4: Create Data Directory


MongoDB stores data in a default directory:
• Linux/macOS: /data/db
• Windows: C:\data\db
If it doesn't exist, create it:
mkdir C:\data\db

Step 5: Start the MongoDB Server


Run this in terminal:
mongod
Open another terminal window and run:
mongo
You're now connected to the MongoDB shell!
Option 2: Cloud-Based – MongoDB Atlas
MongoDB Atlas is the official cloud version of MongoDB, free to
start.
Step 1: Sign Up
• Visit: [Link]
• Create a free account

Step 2: Create a Cluster


• Click "Build a Cluster" → Choose free tier
• Select cloud provider (AWS, GCP, or Azure)
• Choose region (nearest to you)
• Click Create Cluster (takes ~1–2 minutes)

Step 3: Add a Database User


• Go to Database Access → Click "Add New Database User"
• Set username and password (you’ll use it in your app)

Step 4: Whitelist Your IP


• Go to Network Access
• Add IP address: [Link] (allows all IPs during development)

Step 5: Connect to Cluster


• Click "Connect" → Choose "Connect your application"
• Copy the connection URI, which looks like:
mongodb+srv://<username>:<password>@[Link]/myDataba
se
You can use this in your [Link] app (with Mongoose or native driver).
Verify Installation with [Link] + Mongoose (Optional)
Install Mongoose:
npm install mongoose
Example connection:
const mongoose = require('mongoose');

[Link]('mongodb+srv://username:password@[Link]/
mydb')
.then(() => [Link]('Connected to MongoDB Atlas'))
.catch(err => [Link](err));

Summary:

3. Creating a Schema and a Model (with Mongoose)


• Use Mongoose to define schema and interact with MongoDB in [Link].
Install Mongoose:
npm install mongoose
Create Schema and Model:
const mongoose = require('mongoose');

const studentSchema = new [Link]({


name: String,
age: Number,
course: String
});

const Student = [Link]('Student', studentSchema);

4. Connecting to a MongoDB Database:


[Link]('mongodb://localhost:27017/mydb')
.then(() => [Link]("MongoDB connected"))
.catch(err => [Link](err));
Or using Atlas:
[Link]('mongodb+srv://username:password@[Link]/
mydb')

5. Performing CRUD Operations using Express and MongoDB


Create (POST):
[Link]('/students', async (req, res) => {
const student = new Student([Link]);
await [Link]();
[Link](student);
});

Read (GET):
[Link]('/students', async (req, res) => {
const students = await [Link]();
[Link](students);
});
Update (PUT):
[Link]('/students/:id', async (req, res) => {
const student = await [Link]([Link], [Link], {
new: true });
[Link](student);
});

Delete (DELETE):
[Link]('/students/:id', async (req, res) => {
await [Link]([Link]);
[Link]({ message: "Deleted" });
});

You might also like