Open In App

Mongoose | findOneAndDelete() Function

Last Updated : 20 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The findOneAndDelete() function is used to find a matching document, remove it, and passes the found document (if any) to the callback.

Installation of mongoose module:

  1. You can visit the link to Install mongoose module. You can install this package by using this command.
    npm install mongoose
  2. After installing mongoose module, you can check your mongoose version in command prompt using the command.
    npm version mongoose
  3. After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
    node index.js

Filename: index.js




const mongoose = require('mongoose');
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', {
    name: { type: String },
    age: { type: Number }
});
  
// 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);
    }
});


Steps to run the program:

  1. The project structure will look like this:
  2. Make sure you have installed mongoose module using following command:
    npm install mongoose
  3. Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below:
    Database
  4. Run index.js file using below command:
    node index.js

  5. After the function is executed, You can see in the database that the particular user is deleted as shown below:
    new Database

So this is how you can use the mongoose findOneAndDelete() function which finds a matching document, removes it, and passes the found document (if any) to the callback.


Similar Reads

Mongoose Query.prototype.findOneAndDelete() API
The Mongoose Query API findOneAndDelete() method is used to find and delete a single document that is determined from the conditions parameter, from a collection, using the MongoDB query system. Syntax: Query.prototype.findOneAndDelete(conditions, options, callback) Parameters: It accepts the following parameters as mentioned above and described be
3 min read
MongoDB - findOneAndDelete() Method
The findOneAndDelete() method deletes a single document based on the selection criteria from the collection. It deletes the first document from the collection that matches the given filter query expression. It takes five parameters the first parameter is the selection criteria and the others are optional. Syntax: db.Collection_name.findOneAndDelete
2 min read
Mongoose mongoose.model() Function
The mongoose.model() function of the mongoose module is used to create a collection of a particular database of MongoDB. The name of the collection created by the model function is always in plural format mean GFG to gfss and the created collection imposed a definite structure. Syntax: mongoose.model(<Collectionname>, <CollectionSchema>
2 min read
Mongoose | updateMany() Function
The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose
2 min read
Mongoose | deleteOne() Function
The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command.
2 min read
Mongoose | findByIdAndDelete() Function
The findByIdAndDelete() function is used to find a matching document, removes it, and passing the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your m
2 min read
Mongoose | findByIdAndUpdate() Function
The findByIdAndUpdate() function is used to find a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After
2 min read
Mongoose | findOneAndRemove() Function
The findOneAndRemove() function is used to find the element according to the condition and then remove the first matched element. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your mongoose v
2 min read
Mongoose | findOneAndReplace() Function
The findOneAndReplace() function is used to find a matching element, replaces it with the provided element, and passes the returned doc to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you ca
2 min read
Mongoose | countDocuments() Function
The countDocuments() function is used to count the number of documents that match the filter in a database collection. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command.npm install mongoose After installing mongoose module, you can check your mongoose version in th
2 min read