Mongoose - 由 :: 11000 E11000 重复键错误索引引起? [英] Mongoose - caused by :: 11000 E11000 duplicate key error index?

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

问题描述

为什么我会收到这个重复的错误 - Error 创建新用户:WriteError({"code":11000,"index":0,"errmsg":"insertDocument :: 由 :: 11000 E11000 重复键引起错误索引?

所有提供的字段都不为空.

架构:

//声明模式var userSchema = new mongoose.Schema({用户名:{类型:字符串,必填:真,索引:{唯一:真}},密码:{类型:字符串,必填:真},created_on: {type: Date, default: Date.now}});

发布:

//创建 - POST//创建 API 的第一个方法:POST 用于创建新用户.router.post("/", function(req, res, next) {//从 POST 请求中获取值var 用户名 = req.body.username;var 密码 = req.body.password;控制台日志(req.body);//{ 用户名:'tealou',密码:'test123' }//创建新的用户文档用户创建({用户名:用户名,密码:密码},功能(错误,用户){控制台日志(用户);//不明确的如果(错误){console.log("创建新用户时出错:" + err);res.send("创建新用户时出错.");} 别的 {console.log("POST 创建新用户:" + 用户名);res.json(用户);}})});

错误:

<块引用>

创建新用户时出错:WriteError({"code":11000,"index":0,"errmsg":"insertDocument:: 导致by :: 11000 E11000 重复键错误索引:iotdb.users.$name_1 dup键:{:空}","op":{"用户名":"tealou","密码":"$2a$10$7mPGND2FRuJDGnXaVTnkru2.xsGn2Ksf8veBKur4ouD9VUNj60RaC","_id":"5786020088240:"create-30088240:"create-3053000T:28.279Z","__v":0}})

有什么想法吗?

解决方案

您最初在架构中有一个名为 name 的字段,该字段设置为 unique.

我怎么知道?因为错误告诉我:

重复键错误索引:**iotdb.users.$name_1**

您将该字段重命名为 username,但没有删除旧索引.默认情况下,在这种情况下,MongoDB 会将不存在的字段的值设置为 null.

相关文档这里:

<块引用>

如果文档在唯一索引中没有索引字段的值,则索引将为该文档存储空值.由于唯一性约束,MongoDB 将只允许一个缺少索引字段的文档.

要解决这个问题,您需要删除重命名的 name 字段的索引.

Why do I get this duplicate error - Error creating new user: WriteError({"code":11000,"index":0,"errmsg":"insertDocument :: caused by :: 11000 E11000 duplicate key error index?

All the provided fields are not empty at all.

Schema:

// Declare schema
var userSchema = new mongoose.Schema({
    username: {type: String, required: true, index: {unique: true}},
    password: {type: String, required: true},
    created_on: {type: Date, default: Date.now}
});

Post:

// Create - POST
// Create the first method of the API : POST used to create a new user.
router.post("/", function(req, res, next) {
    // Get values from POST request
    var username = req.body.username;
    var password = req.body.password;
    console.log(req.body); // { username: 'tealou', password: 'test123' }

    // Create new user document
    User.create({
        username: username,
        password: password
    }, function(err, user) {
        console.log(user); // undefined
        if (err) {
            console.log("Error creating new user: " + err);
            res.send("Error creating new user.");
        } else {
            console.log("POST creating new user: " + username);
            res.json(user);
        }
    })
});

Error:

Error creating new user: WriteError({"code":11000,"index":0,"errmsg":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: iotdb.users.$name_1 dup key: { : null }","op":{"username":"tealou","password":"$2a$10$7mPGND2FRuJDGnXaVTnkru2.xsGn2Ksf8veBKur4ouD9VUNj60RaC","_id":"5786020088245d33140d6f94","created_on":"2016-07-13T08:55:28.279Z","__v":0}})

any ideas?

解决方案

You initially had a field called name in your schema, that was set to unique.

How do I know? Because of the error telling me so:

duplicate key error index: **iotdb.users.$name_1**

You renamed the field to username, but didn't remove the old index. By default, MongoDB will set the value of a non-existent field to null in that case.

Relevant documentation here:

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.

To solve this, you need to remove the index for the renamed name field.

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

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