node.js - mongoose unique无效
天蓬老师
天蓬老师 2017-04-17 16:15:43
0
2
781
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
迷茫

About the constraints of unique implementation of Document embedded in Field:

Unique index cannot guarantee uniqueness in this case; unique index mainly guarantees the uniqueness of Document Level, but cannot guarantee the uniqueness of Sub Document Level.

So, it is usually necessary to control when operating documents in the code;

However, if I understand your needs correctly:

Is it possible to create a composite unique Index to meet the needs, for example:

Schema is like as blow.

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

Create compound unique index.

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

For reference.

Love MongoDB! Have Fun!

洪涛

I have not tested your code. I added the index option when I used it. You can try it.

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

soonfy

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template