Connecting Node.js to Databases: MongoDB with Mongoose vs SQL with Prisma
A beginner-friendly guide to connecting Node.js with MongoDB using Mongoose and SQL databases using Prisma, with examples and comparisons.

Databases are the backbone of most web applications. They store user accounts, products, orders, blog posts—basically all the data your app needs. In Node.js, we usually connect to databases using ORMs (Object-Relational Mappers) or ODMs (Object-Document Mappers).
In this blog, we’ll explore two popular ways to connect Node.js to databases:
MongoDB with Mongoose (NoSQL world)
SQL Databases with Prisma (SQL world)
We’ll also compare SQL vs NoSQL, show examples of saving and retrieving data, and explain the flow of how a request goes from API → controller → database → response.
1. SQL vs NoSQL: What’s the Difference?
Before diving into code, let’s understand the key difference:
| Feature | SQL (Structured Query Language) | NoSQL (Not Only SQL) |
| Structure | Tables (rows and columns) | Collections (documents in JSON-like format) |
| Schema | Rigid (must define columns) | Flexible (documents can vary in fields) |
| Example Databases | MySQL, PostgreSQL, SQL Server | MongoDB, CouchDB |
| Relationships | Strong (foreign keys, joins) | Weak (nested objects instead of joins) |
| Best For | Banking, e-commerce, complex relationships | Real-time apps, IoT, flexible data models |
👉 Think of SQL as Excel sheets with strict rules, and NoSQL as a flexible JSON storage box.
2. Connecting Node.js to MongoDB with Mongoose
Mongoose is an ODM (Object Document Mapper) for MongoDB. It helps us define schemas, validate data, and easily interact with MongoDB.
Install Dependencies
npm install mongoose express
Basic Setup
const express = require("express");
const mongoose = require("mongoose");
const app = express();
app.use(express.json());
// 1. Connect to MongoDB
mongoose.connect("mongodb://localhost:27017/myapp")
.then(() => console.log("MongoDB connected"))
.catch(err => console.error(err));
// 2. Define Schema
const userSchema = new mongoose.Schema({
name: String,
email: String,
age: Number,
});
// 3. Create Model
const User = mongoose.model("User", userSchema);
// 4. API: Create User
app.post("/users", async (req, res) => {
const user = new User(req.body);
await user.save();
res.json(user);
});
// 5. API: Get All Users
app.get("/users", async (req, res) => {
const users = await User.find();
res.json(users);
});
app.listen(3000, () => console.log("Server running on port 3000"));
Example Flow
User sends
POST /userswith{ "name": "Alice", "email": "alice@test.com", "age": 25 }.The controller creates a new
Userand saves it to MongoDB.A success response with the saved document is returned.
✅ This is how Mongoose makes MongoDB easy to use!
3. Using Prisma as an ORM in Node.js for SQL Databases
Prisma is an ORM for SQL databases like PostgreSQL, MySQL, and SQLite. It lets you write queries in JavaScript/TypeScript instead of raw SQL.
Install Dependencies
npm install @prisma/client
npm install -D prisma
Initialize Prisma:
npx prisma init
Define Schema (prisma/schema.prisma)
datasource db {
provider = "postgresql" // can be "mysql" or "sqlite"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
name String
email String @unique
age Int?
}
Run migration:
npx prisma migrate dev --name init
Use Prisma in Node.js
const express = require("express");
const { PrismaClient } = require("@prisma/client");
const app = express();
const prisma = new PrismaClient();
app.use(express.json());
// 1. Create User
app.post("/users", async (req, res) => {
const user = await prisma.user.create({
data: req.body,
});
res.json(user);
});
// 2. Get All Users
app.get("/users", async (req, res) => {
const users = await prisma.user.findMany();
res.json(users);
});
app.listen(3000, () => console.log("Server running on port 3000"));
✅ Prisma automatically generates a type-safe client for interacting with your SQL database.
4. Code Flow: API → Controller → Database → Response
Here’s how the flow looks in both Mongoose and Prisma:
Client Request (POST /users)
↓
Express Controller
↓
Database ORM (Mongoose or Prisma)
↓
Database (MongoDB or SQL)
↓
Response JSON to Client
5. Diagram Ideas
MongoDB (Mongoose ODM)
Collection: Users
{
_id: 1,
name: "Alice",
email: "alice@test.com",
age: 25
}
SQL (Prisma ORM)
Table: Users
+----+-------+-----------------+-----+
| id | name | email | age |
+----+-------+-----------------+-----+
| 1 | Alice | alice@test.com | 25 |
+----+-------+-----------------+-----+
6. Key Takeaways
Mongoose is great for MongoDB (flexible JSON-like documents).
Prisma is powerful for SQL databases (structured tables with relationships).
SQL vs NoSQL: Choose based on project needs.
Both libraries simplify database work in Node.js by avoiding raw queries.
👉 If your app needs flexible, schema-less data, go with MongoDB + Mongoose.
👉 If your app needs structured, relational data, go with SQL + Prisma.
✅ That’s it! Now you know how to connect Node.js apps to MongoDB with Mongoose and SQL databases with Prisma.



