Vee6
Vee6

Reputation: 1577

$pull function of Waterline ODM for updating Mongo database

Sails' Waterline lets you declare an attribute of an entity as being of type 'array':

module.exports = {
  attributes: {
    stuff: { type: 'array' }
  }  
}

In mongodb there is the $pull operator which can be used in update queries and lets you remove certain values out of array attributes in multiple documents in a single query. From what I've searched I did not find anything which would enable this in Waterline, has anyone found a solution to this or a similar problem? Thanks in advance.

Upvotes: 1

Views: 245

Answers (1)

Vee6
Vee6

Reputation: 1577

This is the way I handled the problem. It is ok now as I am locked to mongodb anyway from various other db calls. Suppose I have the 'Entity' database model and the array attribute is 'arr'. The connection for the model must necessarily be to mongodb:

Entity.native(function (err, collection) {
  //handle err
  collection.update(objectsToUpdate, {
    $pull: { arr: myValue }
  }, function (err, result) {
    //do callback, etc.
  });
});

Upvotes: 0

Related Questions