Skip to main content

How to perform CRUD operations using Node.js and MongoDB?

CRUD with Node.js and MongoDB

To perform CRUD (Create, Read, Update, and Delete) operations using Node.js and MongoDB, we can follow these steps:

1. Install Node.js

To install Node.js, visit https://nodejs.org/en/. The node version used in this code is v16.15.0.

2. Install the MongoDB driver

First, create a project directory. We will name our project "CrudApp". Then, to install the MongoDB driver, use npm by running the following command:

npm install mongodb

For the database view, you can download MongoDB Compass, or MongoDB Atlas can also be used.

3. It's time to CODE

   
    // Node.js has a built-in module called HTTP. This helps
    // Node.js to send information using the
    // Hyper Text Transfer Protocol (HTTP).
    const http = require('http');

    // url module provides utilities for URL resolution and parsing.
    // We are using variable as nodeUrl because we have already
    // defined url variable below
    const nodeUrl = require('url');

    // The MongoClient object provided by the MongoDB driver will
    // be used to create a database connection.
    const { MongoClient } = require('mongodb');


    // Connection URL
    const url = 'mongodb://localhost:27017';

    // Initialize the MongoClient
    const client = new MongoClient(url);

    // Database Name
    const dbName = 'crud_app';

    // Create an async function which will perform operation based
    // on parameters
    async function main(method = 'view', data = false, emailId = false) {

        // Use connect method to connect to the server
        await client.connect();
        console.log('Connected successfully to server');
        const db = client.db(dbName);
        const collection = db.collection('users');
        console.log('method =>', method);
        switch (method) {
            case 'create':
                const insertResult = await collection.insertOne(data);
                return JSON.stringify(insertResult);
            case 'remove':
                const remove = await collection.deleteMany({});
                return 'Removed';
            case 'delete':
                const deleteResult = await collection
                    .deleteOne({ 'email': emailId });
                return JSON.stringify(deleteResult);
            case 'update':
                const updateResult = await collection
                    .updateOne({ 'email': emailId },
                    { $set: data });
                return JSON.stringify(updateResult);
            default:
                return "View"
        }

    }

    //Use it to call the main function with parameters so that
    // we can call this function in just one line according
    // to the URL, and it will return the desired result
    function defineMain(res, method = false, data = false,
        emailId = false) {
        main(method, data, emailId)
            .then(result => {
                res.end(result);
            })
            .catch(err => {
                console.error(err);
                res.statusCode = 500;
                res.end('Internal Server Error');
            });
    }

    // Use this function to create or update the document
    function updateDocument(req, res, method, emailId = false) {
        try {
            let body = '';
            req.on('data', chunk => {
                body += chunk.toString();
            });

            req.on('end', () => {
                const data = JSON.parse(body);
                defineMain(res, method, data, emailId)
            });
        } catch (error) {
            console.error(error);
            res.statusCode = 500;
            res.end('Internal Server Error');
        }
    }

    // create http server
    const server = http.createServer((req, res) => {
        res.setHeader('Content-Type', 'application/json');
        if (req.url === '/create' && req.method === 'POST') {
            updateDocument(req, res, 'create')
        } else if (req.url === '/remove-all') {
            defineMain(res, 'remove')
        } else {
            // Parse the URL and then split it to get the value
            // at index 2, which is the email.
            // This emailId will be used to update or
            // delete the document.
            const urlParts = nodeUrl.parse(req.url, true);
            const emailId = urlParts.pathname.split('/')[2];
            if (urlParts.pathname.startsWith('/delete/')) {
                defineMain(res, 'delete', false, emailId)
            } else if (urlParts.pathname.startsWith('/update/')) {
                updateDocument(req, res, 'update', emailId)
            } else {
                defineMain(res)
            }

        }
    });

    server.listen(3000, () => {
        console.log('Server is listening on port 3000');
    });



 4. The output of the above code



5. GitHub 


Comments

  1. Awesome Tutorial on Crud App using Node.js and MongoDB, thanks for this

    ReplyDelete
    Replies
    1. Thankyou, make sure you also read: https://aks-techies.blogspot.com/2023/02/understanding-the-basics-of-websites.html

      Delete

Post a Comment

Popular posts from this blog

Java Program to count number of employees working in a company (Completed With Garbage Collection)

    // Java Program to count number of employees working in a company (Completed With Garbage Collection)     class Employee {         private String name ;         private int age ;         private int ID ;         private static int nextId = 1 ;                         public Employee ( String name , int age ) {             this . name = name;             this . age = age;             this . ID = nextId ++ ;         }                 public void show ()         {             System . out . println ( " \n ID=" + ID +   " \n Name=" + name + " \n Age=" + age);         }   ...

How to copy one collection to another by adding a field in MongoDB?

Is there a way to copy one collection to another in MongoDB while adding a new field? Yes, it is possible to copy one collection to another in MongoDB by adding a field using the $addFields pipeline stage in the aggregation framework. Here is an example of how you can do it: Suppose we have two collections named source_collection and target_collection , and we want to copy data from source_collection to target_collection by adding a new field called new_field . Sample source_collection documents:     {         "_id" : 1 ,         "name" : "John" ,         "age" : 25     },     {         "_id" : 2 ,         "name" : "Jane" ,         "age" : 30     },     {         "_id" : 3 ,         "name" : "Bob" ,         "age" : 40     } We want to copy this co...