In this tutorial you will learn about the Node.js MongoDB Remove Record and its application with practical example.
Node.js MongoDB Remove Record
The remove() method is used to remove a record from a MongoDB collection, you have to pass search parameters using query object to specify the records to be removed.
Example :- Let’s remove a records from “employees” collection where emp_name is “Keith”.
Step 1:- Let’s, create a node_mongo_remove.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 |
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/nodemongo"; MongoClient.connect(url, function(err, db) { if (err) throw err; var qry = { emp_name: "Keith" }; db.collection("employees").remove(qry, function(err, obj) { if (err) throw err; console.log(obj.result.n + " record(s) deleted"); db.close(); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mongo_remove.js |
you will see following output on terminal –
Output:-
1 |
1 record(s) deleted |