---
title: "Create an Express route to handle deletion of documents from a MongoDB collection."  
description: "Create an Express route to handle deletion of documents from a MongoDB collection."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159309/create-an-express-route-to-handle-deletion-of-documents-from-a-mongodb-collection  
category: "mongodb"  
tags: ["javascript", "mongodb", "express js", "mern"]  
reading_time: 2 minutes  

---

# Create an Express route to handle deletion of documents from a MongoDB collection.

Create an [Express](https://www.mindstick.com/forum/156025/how-to-use-garmin-express-software) [route](https://www.mindstick.com/interview/396/what-is-routed-event-in-wpf) to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) deletion of [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents) from a [MongoDB](https://www.mindstick.com/articles/337565/understanding-document-stores-a-look-at-mongodb-vs-couchdb) collection.

## Replies

### Reply by Aryan Kumar

Sure, here is an Express route to handle deletion of documents from a MongoDB collection:

JavaScript

```plaintext
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/mydb';

const app = express();

app.get('/delete', async (req, res) => {
  const client = await MongoClient.connect(url);
  const db = client.db('mydb');

  const collection = db.collection('people');
  const id = req.query.id;

  const result = await collection.deleteOne({ _id: id });

  if (result.deletedCount === 1) {
    res.status(200).send('Document deleted successfully');
  } else {
    res.status(404).send('Document not found');
  }
});

app.listen(3000, () => console.log('Server started on port 3000'));
```

This route first imports the `express` and `mongodb` modules. It then defines the MongoDB connection URL, which is `mongodb://localhost:27017/mydb`.

Next, the route defines a `get` handler for the `/delete` route. The handler takes a `req` and `res` object as its input. The `req` object contains the query parameters from the request, and the `res` object is used to send the response back to the client.

The `get` handler first connects to the MongoDB database and gets the `people` collection. The handler then gets the `id` of the document to be deleted from the `req` object.

The handler then uses the `deleteOne()` method to delete the document from the collection. The `deleteOne()` method returns a result object, which contains the number of documents that were deleted.

If the document was deleted successfully, the handler sends a `200 OK` response back to the client. Otherwise, the handler sends a `404 Not Found` response back to the client.

To run this route, you can save it as a file called `delete.js` and then run it from the command line:

```plaintext
node delete.js
```

This will start the Express server on port 3000. You can then test the route by making a GET request to `http://localhost:3000/delete?id=1`. If the document with the ID 1 exists in the `people` collection, it will be deleted. Otherwise, a `404 Not Found` response will be returned.


---

Original Source: https://www.mindstick.com/forum/159309/create-an-express-route-to-handle-deletion-of-documents-from-a-mongodb-collection

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
