Unique indexes not working.
See original GitHub issueI’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:
- Created 12 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
markstos, solution work for me. Drop your existing collection and reconnect it again