Mongoose Document Model.prototype.deleteOne() API
The Model.prototype.deleteOne() method of the Mongoose API is used to delete any one document in a collection. This method deletes the first document that matches the condition provided to the method as a first parameter.
Syntax:
Model.prototype.deleteOne()
Parameters: The Model.prototype.deleteOne() method accepts four parameters:
- condition: It is an object to select the document which is going to be deleted.
- options: It is an object with various properties.
- callback: It is a callback function that will run once execution is completed.
Returns: The Model.prototype.deleteOne() function returns a promise. The result contains an object with the property deletedCount indicating how many documents were deleted.
Setting up Node.js application:
Step 1: Create a Node.js application using the following command:
npm init
Step 2: After creating the NodeJS application, Install the required module using the following command:
npm install mongoose
Project Structure: The project structure will look like this:
Database Structure: The database structure will look like this, the following documents are present in the collection.
Example 1: In this example, We have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using deleteOne() method on the User model which will delete one document based on the condition given as a first parameter to the method. In this example, we are selecting the document to be deleted using the “name” field.
- app.js: Write down the below code in the app.js file:
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) const userSchema = new mongoose.Schema( { name: String, age: Number } ) // Defining userSchema model const User = mongoose.model( 'User' , userSchema); //deleteOne() User.deleteOne({ name: 'User2' }).then(result => { console.log(result) }); |
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{ acknowledged: true, deletedCount: 1 }
GUI Representation of the Database using Robo3T GUI tool:
Example 2: In this example, we are selecting the document to be deleted using the “_id” field. Below are the database structure and documents already present in the “users” collection before executing the code for example 2.
- app.js: Write down the below code in the app.js file:
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) const userSchema = new mongoose.Schema( { name: String, age: Number } ) // Defining userSchema model const User = mongoose.model( 'User' , userSchema); //deleteOne() User.deleteOne({ _id: '630dbf8bd614a646d94dfe46' }) .then(result => { console.log(result) }); |
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{ acknowledged: true, deletedCount: 1 }
GUI Representation of the Database using Robo3T GUI tool:
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-deleteOne
Please Login to comment...