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:
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.
soonfy