MongoError,错误:E11000重复键错误 [英] MongoError,err:E11000 duplicate key error

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

问题描述

我有像这样的MongoDb架构

I have a MongoDb schema like this

    var User = new Schema({
    "UserName": { type: String, required: true },
    "Email": { type: String, required: true, unique: true },
    "UserType": { type: String },
    "Password": { type: String }
});

我正在尝试创建新用户
这样做已完成在NodeJs中使用mongoose ODM
这是用于创建的代码:

I am trying to create a new user This is done in NodeJs using mongoose ODM And this is the code for creating:

    controller.createUser = function (req, res) {

    var user = new models.User({
        "UserName": req.body.UserName.toLowerCase(),
        "Email": req.body.Email.toLowerCase(),
        "UserType": req.body.UserType.toLowerCase()
    });
    models.User.findOne({ 'Email': user.Email }, function (err, olduser) {
                    if (!err) {
                        if (olduser) {
                            res.send({ 'statusCode': 409, 'statusText': 'Email Already Exists' });
                        }
                        else if (!olduser) {
                            user.setPassword(req.body.Password);
                            user.save(function (err, done) {
                                if (!err) {
                                    console.log(user);
                                    res.send({ 'statusCode': 201, 'statusText': 'CREATED' });
                                }
                                else {
                                    res.send({ 'Status code': 500, 'statusText': 'Internal Server Error' });
                                }
                            });
                        }
                    }
                    else {
                        res.send({ 'statusCode': 500, 'statusText': 'ERROR' });
                    }
                });
};

创建新用户时,我给出的属性和值如下:

The for creating new user,I am giving attributes and values as follows:

 {
"UserName": "ann",
"Email": "ann@ann.com",
"UserType": "normaluser",
"Password":"123456"
}

我收到这样的错误:

{"Status code":500,"statusText":"Internal Server Error","Error":{"name":"MongoError","err":"E11000 duplicate key error index: medinfo.users.$UserName_1  dup key: { : \"ann\" }","code":11000,"n":0,"connectionId":54,"ok":1}}

我理解这个错误是因为UserName是重复的,但我没有设置具有唯一约束的UserName。每当我添加一个新行时,我只需要电子邮件是唯一的,UserName可以重复。如何实现这个?

I understand that this error is because UserName is duplicated ,but I haven't set UserName with unique constraint.Whenever I add a new row,I need only email to be unique,UserName can be repeated.How to achieve this??

推荐答案

@ManseUK可能是对的,看起来像UserName是'关键' - 在这种情况是指数。 _id 属性是默认创建的主要索引,但是mongodb允许您拥有多个这样的索引。

@ManseUK Is probably right, that looks like UserName is a 'key' - in this case an index. The _id attribute is the "primary" index that is created by default, but mongodb allows you to have multiple of these.

启动mongo控制台并运行 medinfo.users.getIndexes()?有些东西必须在'UserName'上添加一个索引。

Start a mongo console and run medinfo.users.getIndexes()? Something must have added an index on 'UserName'.

required:true 不会这样做,但是你以前可能已经使用过其他设置并且索引尚未删除?

required: true wouldn't do that, but you might have played with other settings previously and the index hasn't been removed?

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

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