how to make a variable a unique key in mongoose?

后端 未结 2 2010
春和景丽
春和景丽 2021-02-20 05:13

For example if I have this schema

var userSchema = mongoose.Schema({
    username: String,
    email: String,
    password         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 05:57

    You can add a constraint with the unique attribute. This will also add a "unique" index for the field to your collection:

    var userSchema = mongoose.Schema({
        username: { type: String, unique: true },
        email: String,
        password: String,
        _todo: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Todo'}]
    });
    

提交回复
热议问题