Skip to content

Instantly share code, notes, and snippets.

@tblobaum
Created September 1, 2011 08:00
Show Gist options
  • Save tblobaum/1185673 to your computer and use it in GitHub Desktop.
Save tblobaum/1185673 to your computer and use it in GitHub Desktop.
Underscore mixins for mongoose
_.mixin({
filterByObject: function (arg, object) {
var obj = {};
for (var a in object) {
if (typeof arg[a] == "object") {
obj[a] = this.filterByObject(arg[a], object[a]);
} else if (arg[a]) {
obj[a] = arg[a];
}
}
return obj;
},
filterByMongooseSchema: function (arg, schema) {
return _.filterByObject(arg, schema.paths);
},
filterMongoosePrivates: function(doc) {
var obj = {};
var keys = _.reject(_.keys(doc._doc), function (key) {
return key.indexOf("_") === 0;
});
for (var i=0,j=keys.length;i<j;i++) {
obj[keys[i]] = doc[keys[i]];
}
return obj;
},
filterMongoosePrivatesArray: function(docs) {
var arr = new Array();
for (var i=0,j=docs.length;i<j;i++) {
arr.push(_.filterMongoosePrivates(docs[i]));
}
return arr;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment