MongoDB difference between error code 11000 and 11001

后端 未结 2 1921
抹茶落季
抹茶落季 2021-01-01 12:33

According to this HIGHLY incomplete list http://www.mongodb.org/about/contributors/error-codes/ they\'re both related to duplicate keys. But I was not able to get a

2条回答
  •  时光取名叫无心
    2021-01-01 13:09

    The code 11001 does 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 11001 code either directly.

    The following few lines will show code 11001:

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

    The expected 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:

    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:

    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 E11000 is a bug: https://jira.mongodb.org/browse/SERVER-5978

提交回复
热议问题