NoSQL Database tutorial with examples using CRUD operations
In this tutorial, we will go over how to use a NoSQL database (specifically MongoDB) with CRUD (Create, Read, Update, Delete) operations. MongoDB is one of the most popular NoSQL databases, and it stores data in a flexible, JSON-like format, making it highly suitable for scalable applications.
We'll use Node.js and MongoDB to demonstrate how to perform CRUD operations. The steps will include:
- Setting up MongoDB
- Connecting to MongoDB with Node.js
- Performing CRUD operations using MongoDB in a Node.js environment
Prerequisites
- MongoDB installed: If you don't have MongoDB installed, you can follow MongoDB installation guide.
- Node.js installed: You can download and install Node.js from here.
- A text editor like VSCode, Sublime, or any IDE of your choice.
1. Setting Up MongoDB and Node.js
First, let's set up the project folder and install the necessary dependencies.
1.1. Create Project Folder and Initialize Node.js Project
Open your terminal/command prompt, and execute the following commands:
This will create a new Node.js project with a package.json
file.
1.2. Install Dependencies
You need to install the following dependencies:
- express: To handle HTTP requests.
- mongoose: To interact with MongoDB.
- body-parser: To parse incoming request bodies (useful for POST/PUT requests).
Run this command to install the dependencies:
2. Setting Up MongoDB Database Connection with Mongoose
2.1. Create a MongoDB Connection File
In the root directory, create a file called db.js
to manage the MongoDB connection.
This file sets up a connection to your local MongoDB server running on the default port 27017
.
3. Create a Model for CRUD Operations
Now, we’ll create a Todo
model to interact with the database.
3.1. Create todo.model.js
Inside your project folder, create a models
folder and create a file called todo.model.js
inside it. This file defines the structure of your Todo item.
This schema defines a simple Todo
item with a title
and completed
field. The timestamps
option automatically adds createdAt
and updatedAt
fields to each document.
4. Set Up CRUD Operations in Express
Now that we have our database connected and model defined, let’s create an Express API to handle CRUD operations.
4.1. Create server.js
File
Create a file called server.js
in your root project directory. This file will handle all your routes.
CRUD Endpoints
- Create (
POST /todos
): Adds a new Todo to the database. - Read (
GET /todos
): Fetches all Todos. - Read (by ID) (
GET /todos/:id
): Fetches a single Todo by its ID. - Update (
PUT /todos/:id
): Updates a Todo by its ID. - Delete (
DELETE /todos/:id
): Deletes a Todo by its ID.
5. Testing CRUD Operations
5.1. Start the Server
Run the following command to start your Express server:
The server will start on http://localhost:3000
.
5.2. Testing Using Postman or CURL
You can use Postman or CURL to test your API endpoints.
- Create a Todo (POST request)
- URL:
http://localhost:3000/todos
- Body (JSON):
- Get All Todos (GET request)
- URL:
http://localhost:3000/todos
- Get Todo by ID (GET request)
- URL:
http://localhost:3000/todos/{id}
- Update Todo (PUT request)
- URL:
http://localhost:3000/todos/{id}
- Body (JSON):
- Delete Todo (DELETE request)
- URL:
http://localhost:3000/todos/{id}
6. Conclusion
You have now created a simple MongoDB-based CRUD application using Node.js, Express, and Mongoose! Here's a summary of the steps:
- Set up MongoDB and connected it to Node.js using Mongoose.
- Created a Todo model to define the structure of our data.
- Implemented CRUD operations (Create, Read, Update, Delete) in Express routes.
- Used Postman or CURL to test the API.
This is a basic example that can be extended with authentication, validation, and other advanced features as needed.
Comments
Post a Comment