我有一个计划:
const tailSchema = new Schema(
{
personeFor: {
type: String,
required: true
},
title: {
type: String,
unique: true,
maxlength: 220,
required: true,
trim: true
}
},
{ versionKey: false, collection: "tails" }
);
title字段应仅在personeFor字段具有相同值的过道中唯一。 也就是说,如果personeFor:“ firstPerson”,我可以指定标题的值:“ firstTitle”字段-仅一次。但是,如果personeFor:“ secondPerson”字段-那么我可以再次使用title:“ firstTitle”。头衔的唯一性应该放在一个人的前列。 可以描述该方案使其可行吗?
答案 0 :(得分:0)
您必须创建一个unique compound index,以确保元组personFor / title是唯一的。
mongo命令:
db.tailSchema.createIndex(
{ personFor: 1, title : 1},
{ unique: true }
)
或者在猫鼬中:
tailSchema.index({
{ personFor: 1, title : 1},
{ unique: true }
);