mongoose findoneanddelete code example
Example 1: findoneanddelete mongoose
User.findOneAndDelete({age: {$gte:5} }, function (err, docs) {
if (err){
console.log(err)
}
else{
console.log("Deleted User : ", docs);
}
});
Example 2: mongoose findoneandupdate
const doc = await CharacterModel.findOneAndUpdate(
{ name: 'Jon Snow' },
{ title: 'King in the North' },
{ new: true }
);
doc.title;
Example 3: mongoose model
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
Example 4: model mongoose
const modelName = mongoose.model("collectionname", collectionSchema);
const fruitSchma = new mongoose.Schema ({
name: String
});
const Fruit = mongoose.model("Fruit", fruitSchema);