Simple CRUD Operations with Node, Express, and MongoDB
Tags: javascript, webdev, node, mongodb
Published: 2021-12-23 16:41:44

First, we will introduce CRUD, Express, and MongoDB

**Express:** Express is a framework on Node.js for building web applications. It simplifies the server creation process in Node.

**MongoDB:** MongoDB is a database. This is the place where we store data for our websites or applications.

**CRUD:** CRUD is a short form for Create, Read, Update and Delete. It is a set operation where we get data servers to the database. CRUD's methods are  POST, GET, PUT and DELETE.

- **Read (GET)** - Get something
- **Create (POST)** - Make something
- **Update (PUT)** - Change something
- **Delete (DELETE)** - Remove something

If we put CRUD, Express, and MongoDB together into a single diagram, this looks like:



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4prximsabu2aqe7ciplu.png)

**CRUD - READ**
We can get data from the database with to server when we use get method in server-side it is the read operation

**Find one document in database**
We can get one document in database userCollection with email

<pre> app.get('/user/:email', async (req, res) => {
            const email = req?.params?.email;
            const query = { email: email };
            const result = await userCollection.findOne(query);
            res.send(result)
        })</pre>

**Find multiple documents in database**
We can get multiple documents in database userCollection

<pre> app.get('/user', async (req, res) => {
            const result = await userCollection.find({}).toArray();
            res.send(result)
        })</pre>

**CRUD - CREATE**
When we insert any data on the database collection we will use the post method. It is the create operation.

**Insert one document in database**
We can insert one document in database userCollection

<pre>app.post('/user', async (req, res) => {
            const cursor = req.body;
            const result = await userCollection.insertOne(cursor);
            res.send(result)
        })</pre>

**Insert multiple document  in database**
We can insert multiple document in database userCollection

<pre>app.post('/user', async (req, res) => {
            const cursor = req.body;
            const result = await userCollection.insertMany(cursor);
            res.send(result)
        })</pre>

**CRUD - UPDATE**
When we update any data on the database collection we will use the put method. It is the update operation.

**Update one document in database**
We can update one document in database userCollection and set user role admin

<pre>app.put('/user/:email', async (req, res) => {
            const email = req?.params?.email;
            const query = { email: email }
            if (query) {
                const updateDoc = {
                    $set: {
                        role: `admin`
                    },
                };
                const result = await userCollection.updateOne(query, updateDoc);
                res.send(result)
            }
        })</pre>

**Update multiple document in database**
We can update multiple document in database userCollection and set user role client

<pre>app.put('/user', async (req, res) => {
            const filter =  req.body;
             const updateDoc = {
                 $set: {
                      role: `client`
                  },
                const result = await userCollection.updateMany(filter, updateDoc);
                res.send(result)
            }
        })</pre>

**CRUD - DELETE**
When we delete any data on the database collection we will use the delete method. It is the delete operation.

**Delete one document in database**
We can delete one document in database userCollection filter id

<pre>app.delete('/user/:id', async (req, res) => {
            const id = req?.params?.id;
            const query = { _id: ObjectId(id) };
            const result = await allProductsCollection.deleteOne(query);
            res.send(result)
        })</pre>

**Delete multiple document in database**
We can Delete multiple document in database userCollection

<pre>app.delete('/user', async (req, res) => {
            const cursor = req.body;
            const result = await allProductsCollection.deleteMany(cursor );
            res.send(result)
        })</pre>