March 19 2025
How can you connect to MongoDB using mongoose.js in NextJS serverless, and How does it differ from connecting in Express.js!
https://avatars.githubusercontent.com/u/7552965?s=200&v=4
Its absolutely necessary to know basics of Mongoose before starting. If you are just starting from this article, then Mongo and Mongoose, then I will not strongly recommend this article, But I have written it in a beginner friendly way. So you can try. Also I have covered basics of mongoose.js in this article, and will recommend to checkout documentation once.
Mongoose is a popular ODM (Object Data Modeling) library that acts as an Object Document Mapper. If you are pretty new to mongoose and you don't have much of knowledge of it, then in this article you can find absoluete required basics of it.
Make sure that Mongo and Node are already installed in your device.
Since its a Javascipt package, you can install it from node package manager (npm) by executing
bash
npm install mongoose --save
Javascript
/* This code should be enough to connect to mongoDB */
import mongoose from "mongoose"
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/test');
// use await mongoose.connect('mongodb://user:password@127.0.0.1:27017/test');
// if your database has auth enabled
}
OR the way I preffer is
Javascript
import mongoose from "mongoose";
mongoose.connect("mongodb://127.0.0.1:27017/test").
then(()=>{
console.log("Connection Success")
})
.catch((e)=>{
console.log("Connection failed, Error:", e);
handleError(e);
});
Javascript
import mongoose from "mongoose";
mongoose.connect() // Its the main function which does the job
// in the parameter the this function comes the connection URI
// The connection URI tells mongoose where to connect for DB
// mongoose.connect("mongodb://thisisconnectionURI:27017/collectionName")
I hope you are already familiar with mongo terminology, The "test" in the end of the connection uri represents the collection name where connection is to be made.
Treat it as a thumb rule, never to use mongoose.connect alone, Let me explane
Javascript
import mongoose from "mongoose";
mongoose.connect("connectionURI");
Above written code will do the job, But is very dangerous. Never use it in production, Not even In testing. This code "says" to connect to DB, But it doesn't says anything about what if connection fails. For this always use try catch or catch method like how I use in previous example.
But If you try to connect using the above way in NEXTJS then It wont work!!
This was pretty much basics of connecting to MongoDB using mongoose in ExpressJS. Its alot same in NextJS, but has some diffrences. Let us see what are those.
First make sure that mongoose is installed, if not that install it by "npm intall mongoose"
Javascript
import mongoose from "mongoose";
const MONGOURI = "mongoDB://connectingURI:27017/"
let cached = global.mongoose;
if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
export default async function connectDB(){
cached.promise = mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then((mongoose) => mongoose);
cached.conn = await cached.promise;
return cached.conn;
}
If you see clearly, We have cached mongoose connection, caching is very much required cause NextJS is serverless, means that, the function execute on demand, means that the server is not always running and only runs whenever any request is received. A new instance of function is created whenever a request is received
Here if database is cached then connectDB will return the cached connection otherwise will try to establish a new connection and will return it.
If the connection is not cached, then each request creates a new MongoDB connection, leading to connection overflow and performance issues. In such a case, connections should be terminated manually, which can be done as follows:
Javascript
await mongoose.connection.close();
Its never really recommended to close connection in NextJS, due to its serverless nature. Its always recommended to cache the connection. I felt it to be important for you to know, How to close connections, Its not usefull if taken from NextJS prespective but can be pretty much usefull if you are using ExpressJS. Its better to gracefully close connection at the time of server shutdown.
This above code can be pasted in database.js and then can be imported in your NextJS application like
Javascript
// app/api/route.jsx
import connectDB from "@/database.js"
export default function POST(){
connectDB(); // should be used inside of request handler function
.
.
If connectDB is called outside of handler function then it will run every time the API file is imported, even if the request is not made. This can lead to multiple connections being created unnecessarily.
Calling connectDB() inside the function ensures the connection is only established when a request is received.
NextJS is, Serverless. So function run per request, so connections need caching.
Express runs as a long-lived server, keeping connections persistent.
In Next.js, connections should be established inside API routes using a cached function. This prevents multiple new connections per request.
In Express, the database connection is typically established once at server startup and reused for all incoming requests.
In Next.js, frequent API function invocations may lead to excessive database connections if caching is not used properly.
In Express, a single persistent connection is maintained, reducing overhead and improving performance.
Next.js API routes are ephemeral, meaning a new function instance may be created for every request, requiring efficient connection handling.
Express runs continuously, keeping database connections alive throughout the application's lifecycle without requiring reconnections.
© 2025 baltej.me. All rights reserved.