Skip to content Skip to sidebar Skip to footer

Removing Documents From A Mongodb Collection From Node.js

I'm totally new to mongoDB and not experienced with Node.js so please excuse if the code below is far from perfect. The goal is to remove a document from a collection, referenced b

Solution 1:

Welcome to asynchronous style:

  • You should not use throw for callback, throw is good for function stack
  • db.close() should be in the callback, after removing is done.

Example:

MongoClient.connect('mongodb://localhost/mochatests', function(err, db) {
    db.collection('contacts', {}, function(err, contacts) {
        contacts.remove({_id: ObjectID("52b2f757b8116e1df2eb46ac")}, function(err, result) {
            if (err) {
                console.log(err);
            }
            console.log(result);
            db.close();
        });
    });
});

Post a Comment for "Removing Documents From A Mongodb Collection From Node.js"