Skip to content

Instantly share code, notes, and snippets.

@kkozionov
kkozionov / mongoose-unique-ref.js
Created May 5, 2016 01:02 — forked from jwietelmann/mongoose-unique-ref.js
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));
});
};