admin管理员组文章数量:1221396
I've got a problem with creating unique indexes using Mongoose / MongoDb and can't get it to 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
Addtion >>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);
});
}...
<<
var Something = new Schema({
objectId : ObjectId,
name : { type : String, index: { unique: true }},
url : { type : String, index: { unique: true }},
...etc
mongoose.model('Something', Something);
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: { : "" } 1ms
When I checked in MongoHub the indexes don't appear, so they don't look like they have been created.
This is a duplicate of this question, but it doesn't have an answer that works for me.
I've got a problem with creating unique indexes using Mongoose / MongoDb and can't get it to 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
Addtion >>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);
});
}...
<<
var Something = new Schema({
objectId : ObjectId,
name : { type : String, index: { unique: true }},
url : { type : String, index: { unique: true }},
...etc
mongoose.model('Something', Something);
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.
This is a duplicate of this question, but it doesn't have an answer that works for me.
Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked May 8, 2011 at 18:44 LewisLewis 5,8796 gold badges33 silver badges41 bronze badges3 Answers
Reset to default 8one solution that doesn't involve erasing your db, is to remove any duplicates manually, then run something along the lines of:
db.users.ensureIndex({email:1},{unique:true,sparse:true});
from the mongo shell
It looks as though the indexes are failing to create because there's already duplicate data in the MongoDB collection. If possible, try deleting all the data and start again with an empty collection.
Since you want to apply unique index in your collection, I suggest you to delete any duplicate documents. Here is the code for doing it:
In Mongo shell client:
db.agencies.ensureIndex({name: 1}, {unique: true, dropDups: true});
db.agencies.ensureIndex({url: 1}, {unique: true, dropDups: true});
本文标签: javascriptUnique index not working with MongooseMongoDBStack Overflow
版权声明:本文标题:javascript - Unique index not working with MongooseMongoDB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739326971a2158294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论