TGViewer
Web Development Web Development @webdevcoursefree ยท 79.6K subscribers
Post #3596 4.77K
๐Ÿ—„๏ธ Database Integration โ€” MongoDB with Node.js

Now you move from temporary data (arrays) โ†’ real database storage.

Backend apps must store data permanently.

That's where databases come in.

๐Ÿง  What is a Database

A database stores data persistently.

Examples:
โ€ข E-commerce: Products, orders
โ€ข Social media: Users, posts
โ€ข Banking app: Transactions

Without database โ†’ data disappears when server restarts.

๐Ÿƒ What is MongoDB

MongoDB is a NoSQL database.

Instead of tables โ†’ it stores documents (JSON-like data).

Example document:
{
  "name": "Deepak",
  "role": "Developer",
  "age": 25
}

Collection = group of documents 
Database = group of collections

๐Ÿ“ฆ Why MongoDB is Popular

โœ… JSON-like data 
โœ… Flexible schema 
โœ… Works perfectly with JavaScript 
โœ… Scales easily

Common in MERN stack.

MERN = MongoDB + Express + React + Node

๐Ÿ”— Connecting MongoDB with Node.js

We use a library called Mongoose.

Install:
npm install mongoose

โšก Step 1 โ€” Connect Database

Example:
const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/myapp")
.then(() => console.log("MongoDB Connected"))
.catch(err => console.log(err));

Now Node server is connected to MongoDB.

๐Ÿงฉ Step 2 โ€” Create Schema

Schema defines data structure.

Example:
const userSchema = new mongoose.Schema({
  name: String,
  age: Number
});

๐Ÿ“„ Step 3 โ€” Create Model

Model allows database operations.

const User = mongoose.model("User", userSchema);

โž• Step 4 โ€” Create Data

app.post("/users", async (req, res) => {
  const user = new User({
    name: req.body.name,
    age: req.body.age
  });

  await user.save();
  res.json(user);
});

๐Ÿ” Step 5 โ€” Fetch Data

app.get("/users", async (req, res) => {
  const users = await User.find();
  res.json(users);
});

โŒ Step 6 โ€” Delete Data

app.delete("/users/:id", async (req, res) => {
  await User.findByIdAndDelete(req.params.id);
  res.json({ message: "User deleted" });
});

โœ๏ธ Step 7 โ€” Update Data

app.put("/users/:id", async (req, res) => {
  const user = await User.findByIdAndUpdate(
    req.params.id,
    req.body,
    { new: true }
  );
  res.json(user);
});

๐Ÿ”„ Full Backend Flow Now

React โ†’ API request 
Express โ†’ Handles route 
Mongoose โ†’ Talks to MongoDB 
MongoDB โ†’ Stores data

โš ๏ธ Common Beginner Mistakes

โ€ข Forgetting to install mongoose
โ€ข Not using async/await
โ€ข Wrong MongoDB URL
โ€ข Not validating schema

๐Ÿงช Mini Practice Task

Build Product API with MongoDB

Routes:
โ€ข POST /products
โ€ข GET /products
โ€ข PUT /products/:id
โ€ข DELETE /products/:id

Fields: 
name 
price 
category

โœ… Double Tap โ™ฅ๏ธ For More
  • โค 34
More from @webdevcoursefree
  1. Oct 14, 2026๐ŸŒ Web Development Roadmap โ€” Part 17 HTML Images โ€” <img> Tag Images make webpages more visโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026For example: Go to Profile โ†’ <a> Submit Form โ†’ <button> Open Article โ†’ <a> Delete Accountโ€ฆ
  4. Oct 12, 2026๐ŸŒ Web Development Roadmap โ€” Part 16 HTML Links โ€” Tag Links are one of the most importantโ€ฆ
  5. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  6. Oct 9, 2026Post #4079
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’