0

I have a database of the students I have a collection in which I only want to insert unique values in my array. My schema looks like this:

var classSchema = Schema({
  class: { type: String, required: true, unique: true },
  name : { type : String },
  ...
  students: [{
    name: String,
    dob: { type: Date, unique: true, sparse: true, required: true }
  }]
});

I want the dob to be unique. When I create new students I am able to create them with the same dob. This is how I am adding it into the database:

{$addToSet:
      {
        "students": {
          name: data.name,
          dob: data.dob
        }
      }
    }, {upsert: true}, function(...) ...
hashing
  • 3
  • 1
  • 2
    Possible duplicate of [Mongoose Unique index not working!](http://stackoverflow.com/questions/5535610/mongoose-unique-index-not-working) – Manasov Daniel Jan 06 '16 at 09:25
  • A unique index won't help you here, see http://stackoverflow.com/questions/14527980/can-you-specify-a-key-for-addtoset-in-mongo – JohnnyHK Jan 06 '16 at 14:20

0 Answers0