Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js & Waterline ORM unset key MongoDB

What method is used to unset a key in MongoDB with Waterline ORM?

Consider the following document:

{
    name : 'brian',
    age : 29
}

Getting the user is no problem:

var users = Users.findOne({ name : 'brian' }).exec(cb);

I would like age to simply go away. I've tried the following to accomplish this:

user.age = undefined;
user.save();

user.age = null;
user.save();

delete user.age;
user.save();

None seem to work. #1 sets it to null, #2 sets it to null, #3 leaves the original value.

Thanks.

like image 983
brian Avatar asked Aug 18 '14 20:08

brian


1 Answers

Waterline is an ORM meant to support a wide range of datastores, so it doesn't support methods that are particular to just MongoDB. You can always access the underlying MongoDB driver using the .native() method, then do anything you want (see the MongoDB node driver docs for available methods):

User.native(function(err, collection) {

    collection.findAndModify(
        {name: 'brian'}, 
        {$unset: {age: true}}, 
        function (err, object) {
           // Continue...
        }
    );
})
like image 81
sgress454 Avatar answered Sep 28 '22 04:09

sgress454