Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

mongoose unique无效

var groupSchema = new mongoose.Schema({
    group: {
        type: String,
        unique: true
    }
});

在存数据的时候,同样的value可以无限存啊


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

关于Field中嵌入的Document实现unique的约束:

unique index不能保证这样情况下的唯一性;unique index主要保证Document Level的唯一性,不能保证Sub Document Level的唯一性。

所以,通常需要代码中操作文档的时候进行控制;

不过,如果您的需求,我理解正确的话:

是否可以创建复合的唯一Index来满足需求,例如:

Schema is like as blow.

{name : String,
group : { type : String}}

Create compound unique index.

.createIndex({name : 1 , "group.type" : 1 } , { unique : true })

供参考。

Love MongoDB! Have Fun!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...