1

I have a schema that looks like this:

var ExampleSchema = new Schema({
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  subdocArr: [{
   date_created: {type: Date, default: Date.now},
   ...
  }]
});

I want the objects in subdocArr to be unique by date_created. E.g. if an object already exists in subDocArr with the same date, the save should be rejected.

I've tried adding an index unique to date_created but failing the tests.
Is there any way to guarantee uniqueness in this scenario on db side? or would I have to defer to program side?

user1203349
  • 277
  • 6
  • 14

0 Answers0