Sure, here is an Express route to handle deletion of documents from a MongoDB collection:
JavaScript
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:
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is an Express route to handle deletion of documents from a MongoDB collection:
JavaScript
This route first imports the
expressandmongodbmodules. It then defines the MongoDB connection URL, which ismongodb://localhost:27017/mydb.Next, the route defines a
gethandler for the/deleteroute. The handler takes areqandresobject as its input. Thereqobject contains the query parameters from the request, and theresobject is used to send the response back to the client.The
gethandler first connects to the MongoDB database and gets thepeoplecollection. The handler then gets theidof the document to be deleted from thereqobject.The handler then uses the
deleteOne()method to delete the document from the collection. ThedeleteOne()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 OKresponse back to the client. Otherwise, the handler sends a404 Not Foundresponse back to the client.To run this route, you can save it as a file called
delete.jsand then run it from the command line: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 thepeoplecollection, it will be deleted. Otherwise, a404 Not Foundresponse will be returned.