node.js mongodb mongoose 中的 E11000 重复键错误索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24430220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
E11000 duplicate key error index in mongodb mongoose
提问by Trialcoder
Following is my userschema in user.jsmodel -
以下是我user在user.js模型中的模式 -
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, unique: true },
password: { type: String, require:true },
},
facebook: {
id : { type: String },
token : { type: String },
email : { type: String },
name : { type: String }
}
});
var User = mongoose.model('User',userSchema);
module.exports = User;
This is how I am using it in my controller -
这就是我在控制器中使用它的方式 -
var user = require('./../models/user.js');
This is how I am saving it in the db -
这就是我将它保存在数据库中的方式 -
user({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){
if(err)
res.send(err);
else {
console.log(result);
req.session.user = result;
res.send({"code":200,"message":"Record inserted successfully"});
}
});
Error-
错误-
{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1 dup key: { : null }"}
I checked the db collection and no such duplicate entry exists, let me know what I am doing wrong ?
我检查了 db 集合并且不存在这样的重复条目,让我知道我做错了什么?
FYI - req.body.emailand req.body.passwordare fetching values.
仅供参考 - req.body.email并且req.body.password正在获取值。
I also checked this post but no help STACK LINK
我也检查了这篇文章,但没有帮助堆栈链接
If I removed completely then it inserts the document, otherwise it throws error "Duplicate" error even I have an entry in the local.email
如果我完全删除,那么它会插入文档,否则即使我在 local.email 中有一个条目,它也会抛出错误“重复”错误
回答by RickN
The error message is saying that there's already a record with nullas the email. In other words, you already have a user without an email address.
错误消息是说已经有一个记录null作为电子邮件。换句话说,您已经有一个没有电子邮件地址的用户。
The relevant documentation for this:
相关文档:
If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.
You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.
如果文档在唯一索引中没有索引字段的值,则索引将为该文档存储空值。由于唯一约束,MongoDB 将只允许一个缺少索引字段的文档。如果有多个文档没有索引字段的值或缺少索引字段,则索引构建将失败并出现重复键错误。
您可以将唯一约束与稀疏索引结合起来,从唯一索引中过滤这些空值并避免错误。
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.
稀疏索引仅包含具有索引字段的文档的条目,即使索引字段包含空值。
In other words, a sparse index is ok with multiple documents all having nullvalues.
换句话说,稀疏索引可以用于多个文档都具有null值。
From comments:
来自评论:
Your error says that the key is named mydb.users.$email_1which makes me suspect that you have an index on both users.emailand users.local.email(The former being old and unused at the moment). Removing a field from a Mongoose model doesn't affect the database. Check with mydb.users.getIndexes()if this is the case and manually remove the unwanted index with mydb.users.dropIndex(<name>).
你的错误说,关键是命名mydb.users.$email_1这让我怀疑你有两个索引users.email和users.local.email(前者是旧的和此刻未使用)。从 Mongoose 模型中删除字段不会影响数据库。检查mydb.users.getIndexes()是否是这种情况并使用 手动删除不需要的索引mydb.users.dropIndex(<name>)。
回答by Rick
If you are still in your development environment, I would drop the entire db and start over with your new schema.
如果您仍在开发环境中,我会删除整个数据库并重新开始使用您的新架构。
From the command line
从命令行
? mongo
use dbName;
db.dropDatabase();
exit
回答by Yegor
Check collection indexes.
检查集合索引。
I had that issue due to outdated indexes in collection for fields, which should be stored by different new path.
由于字段集合中的索引过时,我遇到了这个问题,这些索引应该由不同的新路径存储。
Mongoose adds index, when you specify field as unique.
当您将字段指定为唯一时,Mongoose 添加索引。
回答by Daksh Miglani
Well basically this error is saying, that you had a unique index on a particular field for example: "email_address", so mongodb expects unique email address value for each document in the collection.
嗯,基本上这个错误是说,您在特定字段上有一个唯一索引,例如:“email_address”,因此 mongodb 期望集合中每个文档的唯一电子邮件地址值。
So let's say, earlier in your schema the unique index was not defined, and then you signed up 2 users with the same email address or with no email address (null value).
因此,假设之前在您的架构中未定义唯一索引,然后您使用相同的电子邮件地址或没有电子邮件地址(空值)注册了 2 个用户。
Later, you saw that there was a mistake. so you try to correct it by adding a unique index to the schema. But your collection already has duplicates, so the error message says that you can't insert a duplicate value again.
后来,你看到有一个错误。所以您尝试通过向架构添加唯一索引来纠正它。但是您的集合已经有重复项,因此错误消息说您不能再次插入重复值。
You essentially have three options:
您基本上有三个选择:
Drop the collection
db.users.drop();Find the document which has that value and delete it. Let's say the value was null, you can delete it using:
db.users.remove({ email_address: null });Drop the Unique index:
db.users.dropIndex(indexName)
删除集合
db.users.drop();找到具有该值的文档并将其删除。假设该值为空,您可以使用以下方法删除它:
db.users.remove({ email_address: null });删除唯一索引:
db.users.dropIndex(indexName)
I Hope this helped :)
我希望这有帮助:)
回答by Daggie Blanqx - Douglas Mwangi
I want to explain the answer/solution to this like I am explaining to a 5-year-old , so everyone can understand .
我想解释这个问题的答案/解决方案,就像我向一个 5 岁的孩子解释一样,这样每个人都能理解。
I have an app.I want people to register with their email,password and phone number . In my MongoDB database , I want to identify people uniquely based on both their phone numbers and email - so this means that both the phone number and the email must be unique for every person.
我有一个应用程序。我希望人们使用他们的电子邮件、密码和电话号码进行注册。在我的 MongoDB 数据库中,我想根据他们的电话号码和电子邮件唯一地识别人员 - 因此这意味着电话号码和电子邮件对于每个人都必须是唯一的。
However , there is a problem : I have realized that everyone has a phonenumber but not everyone has an email address .
但是,有一个问题:我意识到每个人都有电话号码,但并不是每个人都有电子邮件地址。
Those that don`t have an email address have promised me that they will have an email address by next week. But I want them registered anyway - so I tell them to proceed registering their phonenumbers as they leave the email-input-field empty .
那些没有电子邮件地址的人向我承诺,他们将在下周之前拥有电子邮件地址。但无论如何我都希望他们注册 - 所以我告诉他们继续注册他们的电话号码,因为他们将电子邮件输入字段留空。
They do so .
他们这样做。
My database NEEDS an unique email address field - but I have a lot of people with 'null' as their email address . So I go to my code and tell my database schema to allow empty/null email address fields which I will later fill in with email unique addresses when the people who promised to add their emails to their profiles next week .
我的数据库需要一个唯一的电子邮件地址字段 - 但我有很多人的电子邮件地址为 'null' 。所以我转到我的代码并告诉我的数据库架构允许空/空电子邮件地址字段,当承诺下周将他们的电子邮件添加到他们的个人资料的人时,我稍后将用电子邮件唯一地址填充这些字段。
So its now a win-win for everyone (but you ;-] ): the people register, I am happy to have their data ...and my database is happy because it is being used nicely ...but what about you ? I am yet to give you the code that made the schema .
所以它现在对每个人都是双赢的(但你 ;-] ):人们注册,我很高兴拥有他们的数据......我的数据库很高兴,因为它被很好地使用......但是你呢?我还没有给你制作 schema 的代码。
Here is the code : NOTE : The sparse property in email , is what tells my database to allow null values which will later be filled with unique values .
下面是代码: 注意:电子邮件中的 sparse 属性告诉我的数据库允许空值,这些空值稍后将用唯一值填充。
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, index:true, unique:true,sparse:true},
password: { type: String, require:true },
},
facebook: {
id : { type: String },
token : { type: String },
email : { type: String },
name : { type: String }
}
});
var User = mongoose.model('User',userSchema);
module.exports = User;
I hope I have explained it nicely . Happy NodeJS coding / hacking!
我希望我已经很好地解释了它。快乐的 NodeJS 编码/黑客攻击!
回答by mad Man
I faced similar issues , I Just clear the Indexes of particular fields then its works for me . https://docs.mongodb.com/v3.2/reference/method/db.collection.dropIndexes/
我遇到了类似的问题,我只是清除了特定字段的索引,然后它对我有用。 https://docs.mongodb.com/v3.2/reference/method/db.collection.dropIndexes/
回答by Sandip Subedi
I had the same issue. Tried debugging different ways couldn't figure out. I tried dropping the collection and it worked fine after that. Although this is not a good solution if your collection has many documents. But if you are in the early state of development try dropping the collection.
我遇到过同样的问题。试过调试不同的方法都想不通。我尝试删除该集合,之后它运行良好。尽管如果您的集合有很多文档,这不是一个好的解决方案。但是,如果您处于开发的早期状态,请尝试删除该集合。
db.users.drop();
回答by Kobe Luo
This is my relavant experience:
这是我的相关经验:
In 'User' schema, I set 'name' as unique key and then ran some execution, which I think had set up the database structure.
在“用户”模式中,我将“名称”设置为唯一键,然后运行一些执行,我认为这已经设置了数据库结构。
Then I changed the unique key as 'username', and no longer passed 'name' value when I saved data to database. So the mongodb may automatically set the 'name' value of new record as null which is duplicate key. I tried the set 'name' key as not unique key {name: {unique: false, type: String}}in 'User' schema in order to override original setting. However, it did not work.
然后我将唯一键更改为“用户名”,并且在将数据保存到数据库时不再传递“名称”值。因此 mongodb 可能会自动将新记录的 'name' 值设置为 null,即重复键。我尝试将“名称”键设置为{name: {unique: false, type: String}}“用户”模式中的非唯一键 ,以覆盖原始设置。但是,它不起作用。
At last, I made my own solution:
最后,我提出了自己的解决方案:
Just set a random key value that will not likely be duplicate to 'name' key when you save your data record. Simply Math method '' + Math.random() + Math.random()makes a random string.
只需设置一个随机键值,当您保存数据记录时,该值不太可能与“名称”键重复。简单的数学方法生成'' + Math.random() + Math.random()一个随机字符串。
回答by bumsoverboard
回答by Embedded_Mugs
I had a similar problem and I realized that by default mongo only supports one schema per collection. Either store your new schema in a different collection or delete the existing documents with the incompatible schema within the your current collection. Or find a way to have more than one schema per collection.
我有一个类似的问题,我意识到默认情况下 mongo 每个集合只支持一个模式。要么将新架构存储在不同的集合中,要么删除当前集合中架构不兼容的现有文档。或者找到一种方法让每个集合拥有多个架构。


