8

I'm creating the schema for a mongo document and I can do everything except prevent duplicates in a non-object array.

I'm aware of the addToSet, but I'm referring to Mongo Schema.

I don't want to check on Update using $addToSet, rather I want this to be part of my schema validation.

Example below.

let sampleSchema = {
   name: { type: 'String', unique: true },
   tags: [{ type: 'String', unique: true }]
}

The above snippet prevents name from having duplicate values. It allows tags to be stored as a string array.

But.. I cannot limit the array to be unique strings.

{ name: 'fail scenario', tags: ['bad', 'bad', 'array']}

I'm able to insert this record which should be a fail scenario.

Proximo
  • 5,566
  • 8
  • 45
  • 63
  • Possible duplicate of [Mongoose Unique values in nested array of objects](http://stackoverflow.com/questions/15921700/mongoose-unique-values-in-nested-array-of-objects) – Karlen Jan 22 '17 at 08:44
  • I'm aware of the $addToSet, but I'm referring to Mongo Schema. – Proximo Jan 22 '17 at 13:13

3 Answers3

8
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const _ = require('underscore');

let sampleSchema = new mongoose.Schema({
  name: {
    type: 'String',
    unique: true
  },
  tags: [{
    type: 'String'
  }]
})
sampleSchema.pre('save', function (next) {
  this.tags = _.uniq(this.tags);
  next();
});
const Sample = mongoose.model('sample', sampleSchema, 'samples');


router.post('/sample', function (req, res, next) {
  const sample = new Sample(req.body);

  sample.save()
    .then((sample) => {
      return res.send(sample);
    })
    .catch(err => {
      return res.status(500).send(err.message);
    })
});
Med Lazhari
  • 588
  • 4
  • 9
  • Thanks for the answer but I'm trying to do this with only Schema, not code and not with flags on save, update methods of mongoose. – Proximo Jan 22 '17 at 14:10
  • In underscore, it's _.uniq not _.unique. Also, here is a revised version that works with references: `this.tags = _.uniq(this.tags, function(i) {return (i._id) ? i._id.toString() : i;});` – BuffMcBigHuge Aug 08 '17 at 17:10
4

I've come to the conclusion that this is impossible to do via Mongoose Schema.

JSON schema is done like so.

let schema = {
   name: { type: 'string' }
   tags: { 
      type: 'array',
      items: { type: 'string', uniqueItems: true }
   }
}

I'll validate with JSON schema before creating Mongo Document.

Proximo
  • 5,566
  • 8
  • 45
  • 63
2

This method builds on Med's answer, handles references, and done completely in scheme validation.

let sampleSchema = new mongoose.Schema({
    strings: [{type: 'String'}],
    references: [{type: mongoose.Schema.Types.ObjectId, ref: 'Reference'],
});

sampleSchema.pre('save', function (next) {

    let sample = this;

    sample.strings = _.uniq(sample.strings, function(i) {return (i._id) ? i._id.toString() : i;});
    sample.references = _.uniq(sample.references, function(i) {return (i._id) ? i._id.toString() : i;});

    return next();

});
BuffMcBigHuge
  • 407
  • 5
  • 7