Search code examples
mongoskin

Mongoskin - Remove Object From Mongo DB Using Parameter Other Than ID


In Mongoskin, I can remove an item from Mongo DB using:

db.collection('/users').removeById(req.body.userid, function(err, result) {
    res.send((result === 1) ? { msg: 'success' } : { msg:'error: ' + err });
});

The above will remove an object based on the user / system specified _id key.

Is there a command to remove all objects by specifying a parameter other than the _id?


Solution

  • You could use the parameterized version of remove:

    db.collection('users').remove({ 'some_field': 'some value' }, callback);
    

    Other than that, you could make it simpler to access by using the provided bind helper:

    db.bind('users', {
       removeByAddress : function(addr, fn){
         this.remove({ address: addr }, fn);
       }
    });
    

    Then, you'd call db.users.removeByAddress('someaddress', callback) and be set.

    Hope this helps!