Today, we delved into two exciting domains: Recursion in DSA and User Models & Schemas in Backend Development! Here's a detailed and creative breakdown of everything we learned. π
Recursion is a powerful tool in programming that allows a function to call itself to solve a problem. It breaks down larger problems into smaller, manageable ones. Let's explore what we covered today:
Recursion is when a function calls itself until a base condition is met. Think of it as solving a problem step by step by reducing its size.
function printHello(n) {
if (n === 0) return; // Base condition
console.log("Hello World");
printHello(n - 1); // Recursive call
}
printHello(5);
function printNumber(n) {
if (n === 0) return; // Base condition
console.log(n);
printNumber(n - 1); // Recursive call
}
printNumber(5);
function printNumberFrom1(n) {
if (n === 0) return; // Base condition
printNumberFrom1(n - 1); // Recursive call
console.log(n);
}
printNumberFrom1(5);
In this session, we learned how to create a User Model and connect it to our MongoDB Database using Mongoose. Letβs dive into the details:
We created a schema that defines the structure of our user documents in MongoDB.
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
const User = mongoose.model("User", userSchema);
module.exports = User;
We connected our backend server to the MongoDB database.
const mongoose = require("mongoose");
mongoose
.connect("mongodb://localhost:27017/myDatabase", { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("β
Connected to MongoDB");
})
.catch((err) => {
console.error("β MongoDB Connection Error:", err);
});
We ensured the server is ready to interact with the database and handle API requests.
const express = require("express");
const app = express();
const mongoose = require("./db/db"); // Database connection file
app.use(express.json());
// Example route
app.get("/", (req, res) => {
res.send("π Welcome to the Backend Server!");
});
app.listen(3000, () => {
console.log("π Server is running on port 3000");
});
- Learned the fundamentals of recursion and how to apply it to real-world problems.
- Solved problems like printing numbers, factorials, and simple iterations using recursion.
- Created a User Model and learned about Schemas in MongoDB.
- Successfully connected to the database and ensured smooth integration.
- Enhanced our understanding of backend workflows with Express.js and Mongoose.
- Explore advanced recursion problems like factorials, Fibonacci series, and maze solving.
- Dive deeper into backend topics like authentication, CRUD operations, and API design.
π₯ Keep Learning, Keep Building! π