Mongoose: Unique is not working

Created on 8 Mar 2017  路  11Comments  路  Source: Automattic/mongoose

HI below is my Schema in which unique is not working

const user = new Schema({
userName: { type: String, required: true, unique: true },
quiz:[]
});

but i have one more Schema in which unique is working and the schema is

const user = new Schema({
userName: { type: 'String', required: true, unique: true },
quiz:[]
});

and the main difference between them is highlighted.
So anyone can tell me what is the issue?
Why i am facing this?

MongoDB Version: v3.4.2
Nodejs Version: v7.4.0
Mongoose Version: v4.8.5

won't fix

Most helpful comment

Not a bug, you have to understand that unique is an index configuration option in your schema. If your 'users' collection doesn't have a unique index on userName, you need to wait for the index to build before you start relying on it.

function exec() {
  const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

  const user = new mongoose.Schema({
    userName: { type: 'String', unique: true },
    quiz: []
  });

  const User = db.model('User', user);

  const doc = new User({
    userName: 'Bob'
  });

  return User.init() // `User.init()` returns a promise that is fulfilled when all indexes are done
    .then(() => User.create(doc))
    .then(() => User.create({ userName: 'Bob' }));
}

While the index is building, MongoDB will still allow duplicates.

All 11 comments

Are you setting the type to the string value 'String'?

yes

Try setting the type to

Schema.Types.String ? I.e.

const user = new Schema({
  userName: { type: Schema.Types.String, unique: true},
});

@nischay30 why? You should be setting it to String

That being said, it looks like this is a bug with [email protected] as well:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5050`;

try {
  exec()
    .then(() => {
      process.exit(0);
    })
    .catch(error => {
      console.error(error);
      process.exit(2);
    })
} catch (error) {
  console.error(error);
  process.exit(2);
}

function exec() {
  const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

  const user = new mongoose.Schema({
    userName: { type: String, unique: true },
    quiz: []
  });

  const User = db.model('User', user);

  const doc = new User({
    userName: 'Bob'
  });

  return User
    .create(doc)
    .then(() => User.create({ userName: 'Bob' }));
}

Not a bug, you have to understand that unique is an index configuration option in your schema. If your 'users' collection doesn't have a unique index on userName, you need to wait for the index to build before you start relying on it.

function exec() {
  const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

  const user = new mongoose.Schema({
    userName: { type: 'String', unique: true },
    quiz: []
  });

  const User = db.model('User', user);

  const doc = new User({
    userName: 'Bob'
  });

  return User.init() // `User.init()` returns a promise that is fulfilled when all indexes are done
    .then(() => User.create(doc))
    .then(() => User.create({ userName: 'Bob' }));
}

While the index is building, MongoDB will still allow duplicates.

This is a bug, it used to work in the older mongoose version but not now.

Can confirm that this error has been fixed in 4.11.3

@vkarpov15 Thanks, you are right :)

Same problem here mongoose: 4.6.1

Same here on version 4.13.12

Use Model.init(). If that doesn't work for you, please open up a new issue with detailed code samples.

function exec() {
  const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

  const user = new mongoose.Schema({
    userName: { type: 'String', unique: true },
    quiz: []
  });

  const User = db.model('User', user);

  const doc = new User({
    userName: 'Bob'
  });

  return User.init() // `User.init()` returns a promise that is fulfilled when all indexes are done
    .then(() => User.create(doc))
    .then(() => User.create({ userName: 'Bob' }));
}

Read more about why this is on the validation docs.

I'm locking this issue for now due to unhelpful comments that obfuscate the answer.

Was this page helpful?
0 / 5 - 0 ratings