How to make user's email unique in mongoDB?

后端 未结 3 1204
刺人心
刺人心 2021-01-01 16:01

I am making a collection of user\'s data. This will be pretty basic consisting of user\'s email, user\'s password (hashed), and an array of strings.

{
    em         


        
相关标签:
3条回答
  • 2021-01-01 16:54

    Use unique keyword, or simply make _id value to be email.

    db.collection.createIndex( { email: 1 }, { unique: true } )
    
    0 讨论(0)
  • 2021-01-01 16:56

    Mongodb ensureIndex has been deprecated, use createIndex instead.

    db.collection.createIndex( { email: 1 }, { unique: true } )

    0 讨论(0)
  • 2021-01-01 16:57

    You can do it by creating a unique index for your email field.

    http://docs.mongodb.org/manual/tutorial/create-a-unique-index/

    0 讨论(0)
提交回复
热议问题