Answers for "mongoose findoneanddelete"

2

findoneanddelete mongoose

// Find only one document matching the
// condition(age >= 5) and delete it
User.findOneAndDelete({age: {$gte:5} }, function (err, docs) {
    if (err){
        console.log(err)
    }
    else{
        console.log("Deleted User : ", docs);
    }
});
Posted by: Guest on May-30-2021
10

mongoose findoneandupdate

// note: this uses async/await so it assumes the whole thing 
// is in an async function 

const doc = await CharacterModel.findOneAndUpdate(
  { name: 'Jon Snow' },
  { title: 'King in the North' },
  // If `new` isn't true, `findOneAndUpdate()` will return the
  // document as it was _before_ it was updated.
  { new: true }
);

doc.title; // "King in the North"
Posted by: Guest on November-01-2020

Code answers related to "mongoose findoneanddelete"

Browse Popular Code Answers by Language