gpt4 book ai didi

node.js - Mongoose - 独立唯一约束

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:46 24 4
gpt4 key购买 nike

我试图在一个架构上获得两个独立的唯一约束,但是......

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com