CRUD in NextJS 13.4 with app router and server actions and MongoDB
In this article, we will explore how to perform CRUD operations using NextJS 13.4 with the app router and server actions, along with MongoDB as the database.
What is CRUD?
CRUD stands for Create, Read, Update, and Delete, which are the four basic functions of persistent storage. In the context of web development, CRUD operations are commonly used to interact with a database to create, retrieve, update, and delete data.
NextJS 13.4
NextJS is a popular and powerful framework for building React applications. With the release of NextJS 13.4, new features have been introduced that make it even easier to build server-rendered and statically generated websites with React.
App Router and Server Actions
The app router in NextJS allows you to define dynamic routes and handle requests directly on the server side. Server actions enable you to perform server-side logic and database operations when handling client requests.
MongoDB
MongoDB is a widely used NoSQL database that provides a flexible and scalable solution for storing and managing data. It is a popular choice for building modern web applications due to its ability to handle large volumes of data and its support for dynamic schema design.
Implementing CRUD in NextJS 13.4 with MongoDB
To implement CRUD operations in NextJS 13.4 with MongoDB, you can start by setting up a connection to your MongoDB database. Once the connection is established, you can create server actions to handle the CRUD operations using the MongoDB Node.js driver.
Example:
const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017'; MongoClient.connect(url, function(err, client) { if (err) throw err; const db = client.db('mydb'); // Create operation db.collection('users').insertOne({ name: 'John', age: 30 }, function(err, res) { if (err) throw err; console.log('1 document inserted'); client.close(); }); // Read operation db.collection('users').findOne({ name: 'John' }, function(err, result) { if (err) throw err; console.log(result); client.close(); }); // Update operation const query = { name: 'John' }; const newValues = { $set: { age: 35 } }; db.collection('users').updateOne(query, newValues, function(err, res) { if (err) throw err; console.log('1 document updated'); client.close(); }); // Delete operation const deleteQuery = { name: 'John' }; db.collection('users').deleteOne(deleteQuery, function(err, obj) { if (err) throw err; console.log('1 document deleted'); client.close(); }); });
Once you have defined the server actions for the CRUD operations, you can then use the app router to create dynamic routes and handle requests from your NextJS application. This allows you to build a powerful and efficient web application that can interact with a MongoDB database to perform CRUD operations.
Conclusion
NextJS 13.4, with its app router and server actions, provides a robust framework for building web applications that can perform CRUD operations. By integrating MongoDB as the database, you can create modern and scalable web applications that can handle large volumes of data efficiently. With the ability to define dynamic routes and handle server-side logic, NextJS 13.4 makes it easier than ever to create web applications that can perform CRUD operations seamlessly.
Why can't I see the registered products in mongodb?
What you just did here, do we call it client side rendering or server side rendering ?
Excelent content! thank you so much for sharing!
Please consider using TS w/ Next 13.4 it would be really helpful
amazing brother,, now it works
– error srcappmodelTodo.js (27:26) @ model
– error TypeError: mongoose.model is not a function
How to resolve this issue?
thanks a lot sir ❤
Hi , thank you for you video, looking forward to your channel
good brother nice effort thanks
Thanks for the video, I joined your channel and I hope it grows fast 🙂
Very nice tutorial kindly make next js 14.3 CRUD with table pagination.
Brother kindly make more project on NextJS 13.4 .