diff options
author | Yuhong Zhang <yuhong.zhang@mongodb.com> | 2022-03-04 20:40:48 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-03-11 06:04:10 +0000 |
commit | a8ce9aab78095ba80a813200b31f179735693332 (patch) | |
tree | d3111c6709e5dea7eba709020ab6ccb2cffbe067 /jstests | |
parent | fe15f13c05c4bfa65f09f6b60931770277785117 (diff) | |
download | mongo-a8ce9aab78095ba80a813200b31f179735693332.tar.gz |
SERVER-61159 Check for duplicates before converting a non-unique index to unique
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/collmod_convert_to_unique.js | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/jstests/core/collmod_convert_to_unique.js b/jstests/core/collmod_convert_to_unique.js index 6647c0e4345..680252e11e7 100644 --- a/jstests/core/collmod_convert_to_unique.js +++ b/jstests/core/collmod_convert_to_unique.js @@ -49,8 +49,13 @@ assert.commandFailedWithCode(db.runCommand({ }), ErrorCodes.BadValue); +// Conversion should fail when there are existing duplicates. assert.commandWorked(coll.insert({_id: 1, a: 100})); assert.commandWorked(coll.insert({_id: 2, a: 100})); +const duplicateKeyError = assert.commandFailedWithCode( + db.runCommand({collMod: collName, index: {keyPattern: {a: 1}, unique: true}}), + ErrorCodes.DuplicateKey); +jsTestLog('Duplicate key error from failed conversion: ' + tojson(duplicateKeyError)); assert.commandWorked(coll.remove({_id: 2})); // Successfully converts to a unique index. |