错误代码 11000 和 11001 之间的 MongoDB 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18032879/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:20:07  来源:igfitidea点击:

MongoDB difference between error code 11000 and 11001

mongodb

提问by Prinzhorn

According to this HIGHLY incompletelist http://www.mongodb.org/about/contributors/error-codes/they're both related to duplicate keys. But I was not able to get a 11001 error. All of the following threw a 11000 error:

根据这个高度不完整的列表http://www.mongodb.org/about/contributors/error-codes/它们都与重复键有关。但是我无法收到 11001 错误。以下所有内容都抛出了 11000 错误:

  • inserting a document with an _idthat already existed
  • inserting a document with duplicate fields where the fields had a compound unique index
  • updating a document with said compound unique index
  • 插入一个_id已经存在的文档
  • 插入具有重复字段的文档,其中字段具有复合唯一索引
  • 使用所述复合唯一索引更新文档

So this goes completely against the linked page, which says 11000 is for _idand 11001 would occur on updates (not inserts).

所以这完全违背了链接页面,它说 11000 用于_id更新(而不是插入)而 11001 会发生。

So my question is: When does 11001 occur?

所以我的问题是:11001 什么时候发生?

回答by Derick

The code 11001does not exist in the 2.5/2.6 branch on GitHub, so if you're trying a 2.5 version than you can't create it. I did have a look at the code, but I can't find any path that shows the 11001code either directly.

该代码11001在 GitHub 上的 2.5/2.6 分支中不存在,因此如果您尝试使用 2.5 版本,则无法创建它。我确实看过代码,但我找不到任何11001直接显示代码的路径。

The following few lines will show code 11001:

以下几行将显示代码11001

db.so.drop();
db.so.insert( { foo: 5 } );
db.so.ensureIndex( { foo: 1 }, { unique: true } );
db.so.insert( { foo: 6 } );

The expected 11000:

预期11000

db.so.insert( { foo: 5 } );
E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }

And now to reach the 11001:

现在到达11001

db.so.insert( { foo: 6 } );
db.so.update( { foo: 6 }, { $set: { foo: 5 } } );
E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }

Still the original 11000, but:

仍然是原始的11000,但是:

db.getPrevError();
{
    "err" : "E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }",
    "code" : 11001,
    "n" : 0,
    "nPrev" : 1,
    "ok" : 1
}

That the original textual error message shows E11000is a bug: https://jira.mongodb.org/browse/SERVER-5978

原始文本错误消息显示E11000是一个错误:https: //jira.mongodb.org/browse/SERVER-5978

回答by nimrodm

Mongo has an ErrorCategoryenum which creates a DUPLICATE_KEY_ERRORfor the following error codes:

Mongo 有一个ErrorCategory枚举,它DUPLICATE_KEY_ERROR为以下错误代码创建一个:

private static final List<Integer> DUPLICATE_KEY_ERROR_CODES = Arrays.asList(11000, 11001, 12582);

The above is from mongodb java driver 3.6.4.

以上来自mongodb java driver 3.6.4。

So both refer to duplicate keys.

所以两者都是指重复的键。