E11000 mongodb mongoose 中的重复键错误索引 [英] E11000 duplicate key error index in mongodb mongoose

查看:22
本文介绍了E11000 mongodb mongoose 中的重复键错误索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在 user.js 模型中的 user 架构 -

Following is my user schema in user.js model -

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"});
    }
});

错误 -

{"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 ?

仅供参考 - req.body.emailreq.body.password 正在获取值.

FYI - req.body.email and req.body.password are fetching values.

我也检查了这篇文章,但没有帮助 堆栈链接

I also checked this post but no help STACK LINK

如果我完全删除,则它会插入文档,否则即使我在 local.email 中有条目,它也会引发错误重复"错误

If I removed completely then it inserts the document, otherwise it throws error "Duplicate" error even I have an entry in the local.email

推荐答案

错误信息是说已经有一条null作为email的记录.换句话说,您已经有一个没有电子邮件地址的用户.

The error message is saying that there's already a record with null as the email. In other words, you already have a user without an email address.

相关文档:

如果文档在唯一索引中没有索引字段的值,则索引将存储该文档的空值.由于唯一性约束,MongoDB 将只允许一个缺少索引字段的文档.如果有多个文档没有索引字段的值或缺少索引字段,则索引构建将失败并出现重复键错误.

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.

唯一索引

稀疏索引仅包含具有索引字段的文档的条目,即使索引字段包含空值.

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.

换句话说,稀疏索引对于多个文档都具有 null 值是可以的.

In other words, a sparse index is ok with multiple documents all having null values.

稀疏索引

来自评论:

您的错误表明密钥名为 mydb.users.$email_1 这让我怀疑您在 users.emailusers 上都有索引.local.email (前者是旧的,目前未使用).从 Mongoose 模型中删除字段不会影响数据库.如果是这种情况,请使用 mydb.users.getIndexes() 检查并使用 mydb.users.dropIndex(<name>) 手动删除不需要的索引.

Your error says that the key is named mydb.users.$email_1 which makes me suspect that you have an index on both users.email and 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>).

这篇关于E11000 mongodb mongoose 中的重复键错误索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆