mongodb mongoose中的E11000重复错误指数 [英] E11000 duplicate key error index in mongodb mongoose

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

问题描述

以下是 user.js 模型中的用户模式 -

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');

这是我如何将其保存在db -

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 ?

FYI - req.body.email req.body.password 正在获取值。

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

我也检查了这篇文章,但没有帮助 STACK LINK

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 的记录。换句话说,您已经拥有没有电子邮件地址的用户。

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 值。

稀疏索引

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

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