javascript - mongodb怎么设置用户的账户不重复?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-02 09:25:53
0
2
696

用户注册有两个属性name和address,怎么设置数据库让这两个属性对应相等而不发生重复。
我的意思就是不希望出现一个address对应多个用户名

谢谢

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(2)
曾经蜡笔没有小新

If I understand correctly, create a unique matching index:

.createIndex( { name: 1, address: 1 }, { unique: true } )

It is recommended to check the official MongoDB manual to improve work efficiency.

Love MongoDB, Have Fun!


巴扎黑
var studentSchema = new Schema({
  school:String,
  nickname:String,
  username:{
    type:String,
    unique:true
  },.....
}

Mine schema is like that, you can try the following and see if it works

name:{
type:String,
unique:true
},
address:{
type:String,
unique:true
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template