H
H
HoHsi2016-01-16 17:49:49
MongoDB
HoHsi, 2016-01-16 17:49:49

How to set unique parameter in MongooseJS?

Good afternoon!
I am doing authorization on the site, and due to inexperience I riveted the following scheme:

schema = new Schema({
  local: {
    /* Имя пользователя */
    username: {
      type: String,
      required: true,
      unique: true, /* <- не проверяется на уникальность */
      index: true,
      trim: true,
      validate: validators.username
    },

    /* Имя пользователя в нижнем регистре*/
    usernameLow: {
      type: String,
      unique: true, /* <- не проверяется на уникальность */
      trim: true
    },

    /* Пароль */
    hashedPassword: {
      type: String
    },
    salt: {
      type: String
    }
  },
  vk: {
    user_id: {
      type: String
    },
    access_token: {
      type: String
    },
    expires_in: {
      type: Number
    }
  },
  general: {
    email: {
      type: String,
      required: false,
      unique: true,
      index: true,
      trim: true,
      validate: validators.email
    },
    role: {
      type: String,
      "default": "user"
    },
    created: {
      type: Date,
      "default": Date.now
    }
  }
});

Why are object fields not checked for uniqueness?
PS is it worth using passportjs (while I look at it with distrust, as it tightly imposes its routing model)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pomeo, 2016-01-16
@pomeo

There is a possibility that the next index: trueone overrides the top one unique: true. https://docs.mongodb.org/manual/core/index-unique/...
You need to remove index: true, remove indexes from the collection, mongoose won't fix them by itself. And try again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question