Skip to content

Instantly share code, notes, and snippets.

@tblobaum
Created September 24, 2011 05:47
Show Gist options
  • Save tblobaum/1239021 to your computer and use it in GitHub Desktop.
Save tblobaum/1239021 to your computer and use it in GitHub Desktop.
var MongooseFilter = function (schema, options) {
options = options || {};
schema.pre('save', function (next) {
this.filter();
next();
});
schema.method('filter', function () {
var self = this;
var paths = schema.paths;
if (!'_id' in paths) paths['_id'] = '';
for (var key in this._doc) {
if (!(key in paths)) {
delete this._doc[key];
if (options.debug) console.log('Warn: Cannot put ' + key + ' in ' + this.constructor.modelName);
}
}
return this;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment