Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kkozionov/44e9a902637b9d7211d9203f91c37369 to your computer and use it in GitHub Desktop.
Save kkozionov/44e9a902637b9d7211d9203f91c37369 to your computer and use it in GitHub Desktop.
Mongoose plugin for keeping unique ObjectId ref Arrays
module.exports = function(schema, options) {
schema.method('addUniqueRef', function(field, item) {
if(this[field].indexOf(item) < 0) this[field].push(item);
});
schema.method('removeUniqueRef', function(field, item) {
this[field].splice(this[field].indexOf(item));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment