Mongoose Document Model.remove() API
The Model.remove() method of the Mongoose API is used to remove all the documents present in the collection in one go. It will be called on a Collection model which is already defined and present in the database.
Syntax:
Model.remove()
Parameters: The Model.remove() method accepts two parameters:
- options: It is an object with various properties.
- callback: It is a callback function that will run once execution is completed.
Return type: The Model.remove() function returns a promise.
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 customerSchema, having three columns or fields “name”, “orderCount”, and “superUser”. In the end, we are using the remove() method on the Customer model which will remove all the documents present in the Customer model. And in the output we will get an object with two properties:- “acknowledged” which states everything works smoothly and “deletedCount” which counts the number of documents deleted.
- app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( 'mongoose' );
mongoose.connect(
useNewUrlParser: true ,
useUnifiedTopology: true
})
const customerSchema = new mongoose.Schema(
{ name: String, orderCount: Number, superUser: Boolean}
)
const Customer = mongoose.model( 'Customer' , customerSchema);
Customer.remove().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: 3 }
GUI Representation of the Database using Robo3T GUI tool:
Example 2: In this example, we are defining usersSchema and User Model. On the User model, we called remove() and at the end, after removing all the records from the database we are searching for a document using the find() method on the User model, in the result we got empty which shows that no records present in the User collection.
- app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( 'mongoose' );
mongoose.connect(
useNewUrlParser: true ,
useUnifiedTopology: true
})
const userSchema = new mongoose.Schema(
{ name: String }
)
const User = mongoose.model( 'User' , userSchema);
User.remove().then(result => {
console.log(result)
})
User.find({_id: '630db5818577bafc2709d603' }).
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: 2 }
[]
GUI Representation of the Database using Robo3T GUI tool:
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-remove
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...