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(...) ...