-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: 1.49.5
-
Component/s: None
-
Environment:OS: macOs tahoe 26.4.1
node.js / npm versions:
Additional info:
-
None
-
None
-
Developer Tools
Problem Statement/Rationale
In the steps to Convert An Existing Index to Unique, step 2 checks for documents in the collection that would prevent the index from being unique with this command:
- db.runCommand(
{collMod: "CollName", index:
Unknown macro: \{keyPattern}
, dryRun: true})
If the collection has documents with duplicated values in the key field(s) that would violate, then:
- if the collMod command was run in the standalone mongosh shell, the _id values of the duplicated documents are returned in an Uncaught MongoServerError CannotConvertIndexToUnique
- if the collMod command was run in the mongosh shell embedded in Compass, a stack trace is returned instead of the _id values of the duplicated documents:
Please be sure to attach relevant logs with any sensitive data redacted.
How to retrieve logs for: Compass; Shell
Steps to Reproduce
// 1. load some data.
// Note that with bot in the colour field are unique, while those in manu_id are not.
> db.cars.insertMany([
{_id: 1, manu_id: 100, colour: 'blue'},
{_id: 2, manu_id: 200, colour: 'gold'},
{_id: 3, manu_id: 300, colour: 'lime'},
{_id: 4, manu_id: 300, colour: 'gray'}
]);
// 2. Create 2 regular indexes
> db.cars.createIndexes([ {manu_id: 1}, {colour: 1} ]);
// 3. Prepare both indexes to become unique indexes
> db.runCommand({collMod: "cars", index: {keyPattern:
{colour: 1}, prepareUnique: true}});
> db.runCommand({collMod: "cars", index: {keyPattern:
{manu_id: 1}, prepareUnique: true}});
// 4. Test {colour: 1} index for documents that would prevent the index from becoming unique:
> db.runCommand({collMod: "cars", index: {keyPattern:
{colour: 1}, unique: true}, dryRun: true});
// expected result for {colour: 1} given values are unique
{ ok: 1, ... }
// 5. Test {manu_id: 1} index for documents that would prevent the index from becoming unique:
> db.runCommand({collMod: "cars", index: {keyPattern:
{manu_id: 1}, unique: true}, dryRun: true});
// expected result for {manu_id: 1} given values are NOT unique
Uncaught:
MongoServerError[CannotConvertIndexToUnique]: Cannot convert the index to unique.
Please resolve conflicting documents before running collMod again.
Violations: [ { ids: [ 3, 4 ] } ]
Results are as expected with the standalone mongosh shell, but command 5. throws a stack trace if run in the mongosh shell embedded in Compass.
Expected Results
Same outputs from both mongosh shells
Actual Results
Stack trace when run via Compass
Additional Notes
Any additional information that may be useful to include.