deleteOne best use? (for single query and delete)


I’m testing out a POST webhook to query a database with an incoming payload, which if the value is found it will remove the entry from the database, if not found it won’t do anything.

The script function I’m using below (adapted from one of your tutorials) seems to be working, but I wanted to know if this is the best approach with deleteOne for what I want to achieve.

(On a few occasions I had it writing the incoming value into the database as four entries instead of removing it once, which made me concerned if I’m using deleteOne in the correct way here, or if the error was perhaps something else)

Many thanks Andi

Function Script:

exports = async function(payload, response) {
    const mongodb ="mongodb-atlas");
    const eventsdb = mongodb.db("mydatabase");
    const eventscoll = eventsdb.collection("mycollection");
    const result= await eventscoll.deleteOne(payload.query);
    var id = result.deletedCount.toString();
    if(result) {
       return JSON.stringify(id,false,false);   
    return { text: `Error saving` };