MongoDB
Introduction to MongoDB
• MongoDB is a NoSQL database that stores data in a flexible, JSON-like format.
• It is document-oriented, meaning data is stored in collections of documents instead of
tables and rows (like in SQL databases).
• Used in modern web applications, especially with [Link], MERN stack, and big data
applications.
MongoDB Installation
1. Download from MongoDB Official Website.
2. Install and start the MongoDB server (mongod).
3. Use the MongoDB shell (mongosh) to interact with the database.
MongoDB Basic Concepts
SQL (Relational DB) MongoDB (NoSQL)
Database Database
Table Collection
Row Document
Column Field
Primary Key _id (Unique Identifier)
Example of a MongoDB Document (JSON format):
json
"_id": "1",
"name": "John Doe",
"age": 25,
"email": "john@[Link]",
"skills": ["JavaScript", "MongoDB"]
MongoDB Commands
1️.Show Databases
MongoDB
sh
show dbs
Lists all databases.
2️ .Create/Use a Database
sh
use myDatabase
Creates (if not exists) or switches to myDatabase.
3️.Show Collections
sh
show collections
Lists all collections in the current database.
4️.Create a Collection
sh
[Link]("users")
Creates a users collection.
5️.nsert Data
sh
[Link]({ "name": "Alice", "age": 28 })
Inserts a single document.
sh
[Link]([
{ "name": "Bob", "age": 30 },
{ "name": "Charlie", "age": 22 }
])
Inserts multiple documents.
6️.Read Data
sh
[Link]()
Retrieves all documents.
sh
[Link]({ "name": "Alice" })
MongoDB
Finds the first document where name is "Alice".
sh
[Link]({ "age": { "$gt": 25 } })
Finds users with age > 25.
7️.Update Data
sh
[Link]({ "name": "Alice" }, { "$set": { "age": 29 } })
Updates a single document.
sh
[Link]({}, { "$set": { "active": true } })
Updates multiple documents.
8️.Delete Data
sh
[Link]({ "name": "Alice" })
Deletes one document.
sh
[Link]({ "age": { "$lt": 25 } })
Deletes multiple documents where age < 25.
MongoDB Query Operators
Operator Description
$eq Equal to
$ne Not equal to
$gt Greater than
$lt Less than
$gte Greater than or equal to
$lte Less than or equal to
$in Matches values in an array
$nin Does not match values in an array
MongoDB
Example:
sh
[Link]({ "age": { "$gte": 25, "$lte": 30 } })
Finds users with age between 2️5️ and 3️0.
🔗 MongoDB Indexing
• Indexes speed up searches.
• Default _id is indexed.
• Create an index on a field:
sh
[Link]({ "email": 1 })
MongoDB with [Link]
To connect MongoDB with [Link], use the mongodb package.
1️.Install MongoDB Driver
sh
npm install mongodb
2️.Connect to MongoDB
javascript
const { MongoClient } = require("mongodb");
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
async function run() {
try {
await [Link]();
[Link]("Connected to MongoDB!");
const db = [Link]("myDatabase");
const users = [Link]("users");
// Insert a document
MongoDB
await [Link]({ name: "John", age: 25 });
// Fetch documents
const allUsers = await [Link]().toArray();
[Link](allUsers);
} finally {
await [Link]();
run();
📌 Why Use MongoDB?
Flexible Schema – No need for a fixed table structure.
Scalable – Works well with large datasets.
Fast Read/Write – Optimized for high performance.
Easy Integration – Works well with JavaScript, [Link], and modern web stacks.