我试图在一个架构上获得两个独立的唯一约束,但是......
var Game = new Schema({
name: String,
shortName: String
})
Game.index({ name: 1, shortName: 1 }, { unique: true })
...始终创建一个复合索引,使下表有效:
name: "Foo", shortName: "Bar";
name: "Foo", shortName: "Foo";
有什么办法可以让这些独特的约束变得独立吗?
最佳答案
来自 unique index documentation :
You can also enforce a unique constraint on compound indexes. These indexes enforce uniqueness for the combination of index keys and not for either key individually.
因此,要实现此目的,您需要在每个字段上创建唯一索引,如下所示:
var Game = new Schema(
{
name: { type: String, unique: true },
shortName: { type: String, unique: true }
}
)
关于node.js - Mongoose - 独立唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43178258/