node mongoose:如何仅在嵌入文档中设置唯一索引

时间:2017-05-21 06:56:14

标签: node.js mongodb mongoose

使用以下组架构,role.name只能在组中唯一吗?我希望能够将同一个角色名称存储到同一组中的另一个组按钮中...

/**
 * 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
  }
});

0 个答案:

没有答案