DEV Community

Cover image for Simple CRUD Operations with Node, Express, and MongoDB
Mst. Poly Khatun
Mst. Poly Khatun

Posted on

Simple CRUD Operations with Node, Express, and MongoDB

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

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

 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)
        })

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

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

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

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

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

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

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

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)
            }
        })

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

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)
            }
        })

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

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)
        })

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

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

Top comments (1)

Collapse
 
romykhan69 profile image
Romy Khan

Helpful