Delete data with DELETE
In this section, you will learn how to remove items from your database using the DELETE method.
Before working on CRUD operations, ensure your Node-RED environment is connected to your MongoDB database.
To build the DELETE method, we will need the following nodes:
| Node | Description |
|---|---|
| http in | Listens to HTTP requests. |
| function | Allows to write custom JavaScript code to manipulate data passed through it. |
| mongodb out | Allows data to be stored, updated, or removed from a MongoDB collection. |
| http response | Replies to HTTP requests. |
-
Drag and drop the nodes onto the dashboard in
the specified order:

-
To connect a node to the next one, click on
the gray square on its right border and drag your mouse to the gray square
on the left border of the next node. You will see a line linking the two
nodes. Now, we are ready to configure each of the nodes.

-
http in:
Double-click the node to open its properties. Set the method to DELETE, name the endpoint for your URL, and then click Done.

-
function:
To update an object, let's identify it by the object ID that MongoDB generates automatically for its records. To do this, open the Setup tab in the function's properties, add a module, and set it to objectid.

Return to the On Message tab and enter a descriptive name for your function.
Now, let's modify the function's body to meet our needs. We will pass the _id property received in the body of the http request in msg.payload. To do this, insert the following code at the beginning of the function's body:
msg.payload = { _id: objectid(msg.payload._id), };Your function should now appear as follows:

-
mongodb out:
Specify the collection name and set the operation to remove. Click Done.

- http response: Leave the node without any changes.
- Deploy the service.
Your API can now remove data from your database using the DELETE method.
To test your REST API, you can use a platform like Postman.
