search

Home  >  Q&A  >  body text

node.js - mongoose unique无效

天蓬老师天蓬老师2778 days ago782

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 16:17:43

    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!

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:17:43

    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

    reply
    0
  • Cancelreply