Remove

In MongoDB, you can delete records or documents by using the remove() method. The first parameter of the remove() method is a query object which specifies the document to delete.

Example

Remove the record of employee whose address is Ghaziabad.

Create a js file named "remove.js", having the following code:

snippet
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myquery = { address: 'Ghaziabad' };
db.collection("employees").remove(myquery, function(err, obj) {
if (err) throw err;
console.log(obj.result.n + " record(s) deleted");
db.close();
});
});

Open the command terminal and run the following command:

snippet
Node remove.js
Node.js Remove 1

Verification

You can check that the record having address "Ghaziabad" is deleted and only following records are available now:

Node.js Remove 2
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +