Q:

How do I give "document does not exist" feedback to the user when using the Mongoose deleteOne function?

I'm using NodeJS, Commander, and Mongoose to create a simple CLI. If my delete function is called and it does not find a document in the database, I'd like it to say "Document does not exist." However, whenever I call the function, it always runs the else code and says document has been deleted. Even with 0 documents in the database. How do I give the proper feedback to the user?
  const deleteNote = (label) => {
  const search = new RegExp(label, 'i');
  Note.deleteOne({'label': search,},
  function(error, doc) {
    if (error || !doc) {
      console.log('Document does not exist');
      mongoose.connection.close();
    } else {
      console.log(`Document deleted`);
      mongoose.connection.close();
}});};

A:

Use the second argument from callback.
const mongoose = require("mongoose")
const Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/stackoverflow', {useNewUrlParser: true});

const NoteSchema = new Schema({
    title: String,
    label: String
})
const NoteModel = mongoose.model("Note", NoteSchema)

function run(label) {
    NoteModel.deleteOne({ label }, (err,result)=>{
        console.log(result) // { n: 0, ok: 1, deletedCount: 0 }
        // deletedCount shows number of docs deleted
        if(result.deletedCount >  0) {
            console.log("DELETED")
        } else {
            console.log("NOT DELETED")
        }
    })            
}

run("Label 2")

