1 Sep 2013

Remove a MongoDB document using node.js and mongojs

Remove (delete) a document from a MongoDB database using the mongojs module and node.js.

Have you completed the getting started with Mongo steps?

If you haven't already, install the mongojs module using npm:

$ sudo npm install mongojs

Edit server.js:

var http = require("http");
var mongo = require("mongojs");

var db = mongo.connect("shop", ["products"]);

http.createServer(function(req, res) {

  var query = {
    // WHERE make = 'Samsung'
    make: "Samsung"
  };

  db.products.remove(query, function(err, result) {
    if(err) { throw err; }
    res.end("<p>Product removed</p>");
  });

}).listen(8080);

Start the node instance:

$ node server.js

Test in your browser:

http://localhost:8080
Product removed