Saturday, 20 May 2017

node mongoose : how to set unique index only within an embedded document

Using the following Group Schema , will the role.name be unique IN the group only ? I would like to be able to store the same role name into another group button in the same group ...

/**
 * Role Schema
 */
const Role = new mongoose.Schema({
  name: { type: String, required: true, unique: true },
  description: { type: String, required: false }
});

/**
 * Group Schema
 */
const GroupSchema = new mongoose.Schema({
  name: { type: String, index: { unique: true, required: true, dropDups: true } },
  description: { type: String, required: false },
  roles: [Role],
  createdAt: {
    type: Date,
    default: Date.now
  }
});



via erwin

No comments:

Post a Comment