question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique indexes not working.

See original GitHub issue

I’ve got a problem with creating unique indexes using Mongoose / MongoDb and can’t get it work, I’m able to add two documents with the same attribute values when I have set a unique index.

I’ve tried everything I can think of - restarting (everything) changing the syntax etc.

code

    var Something = new Schema({
    objectId          : ObjectId,
    name              : { type : String, index: { unique: true }}, 
    url               : { type : String, index: { unique: true }},
    ...etc
    mongoose.model('Something', Something);

This is the method that I’m using to save an entity:

create  : function(entity, definition, successFn, errorFn){ 

var model = mongoose.model(entity);
newModel = new model(definition);

newModel.save(function(error) {
  if(error){
    if(!errorFn){
      throw error;
    }
    errorFn(newModel);
    return;
  }

  successFn(newModel);
});

}…

Mongo output

 [conn1] insert xxxxx.agencies 1526ms
 [conn1] building new index on { name: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error    index: xxxxx.agencies.$name_1  dup key: { : "something" } 4ms
 [conn1] building new index on { url: 1 } for xxxxx.agencies
 [conn1] insert xxxxx.system.indexes exception 11000 E11000 duplicate key error index: xxxxx.agencies.$url_1  dup key: { : "http://www.something.com" } 1ms

When I checked in MongoHub the indexes don’t appear, so they don’t look like they have been created.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
markstoscommented, Sep 16, 2015

Perhaps what happened is that the index were first created non-unique and later the unique attribute was added, but Mongoose didn’t create the index as unique indexes because it saw the indexes already existed.

0reactions
snanilimcommented, Sep 22, 2018

markstos, solution work for me. Drop your existing collection and reconnect it again

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Unique index not working! - Stack Overflow
Unique seems to work on one of my hosts, but fails to enforce uniques using exactly same node/mongoose code on a different host....
Read more >
Solved! Mongoose Unique Index Not Working
I was working on a project for an organisation I volunteer for, I encountered some difficulties in making the email field in my...
Read more >
Unique partial index is not working with mongoose on email
I tried creating the index manually in compass, it doesn't work either. I have tried every solution on stack overflow without any success....
Read more >
Unique indexes not working · Issue #56 · Automattic/mongoose
Hi, I'm trying to use model indexes with unique properties, but it's not working as expected by the native ensureIndex method from MongoDB....
Read more >
Unique and non-unique indexes - IBM
Non-unique indexes are not used to enforce constraints on the tables with which they are associated. Instead, non-unique indexes are used solely to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found