search

Home  >  Q&A  >  body text

javascript - How to set user accounts not to be duplicated in mongodb?

User registration has two attributes, name and address. How to set up the database so that these two attributes are equal without duplication.
What I mean is that I don’t want one address to correspond to multiple usernames

Thank you

曾经蜡笔没有小新曾经蜡笔没有小新2804 days ago732

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-02 09:27:53

    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!


    reply
    0
  • 巴扎黑

    巴扎黑2017-05-02 09:27:53

    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
    }

    reply
    0
  • Cancelreply