Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-15151

[SERVER] Convert a non-unique index to a unique index via the collMod command

      Original Downstream Change Summary

      In MongoDB 6.0, we introduce the ability to convert a non-unique index to a unique one via a two phase process with the collMod command.

      The first step is as follows given an index of the "a" field:
      {collMod: collName, index: {keyPattern:

      {a: 1}

      , prepareUnique: true}}

      The prepareUnique flag puts the regular index into a state where it will start rejecting all new duplicate entries. These new violations will fail with DuplicateKey errors. This behavior is similar to unique indexes, but it will not be marked as "unique" in the durable catalog.

      The second (and final) step:
      {collMod: collName, index: {keyPattern:

      {a: 1}

      , unique: true}}

      The unique: true (without dryRun: true) flag actually scans through the index to ensure there are no duplicate entries. If duplicates are detected, collMod will return a CannotConvertIndexToUnique error with a list of conflicting documents. The user is expected to fix these errors before continuing with the conversion.

      If the user wants to abort the conversion, they may pass in prepareUnique: false.

      Description of Linked Ticket

      This should include making the necessary catalog changes, but does not encompass checking for duplicates. We should avoid bumping the data format version so that there aren't any upgrade/downgrade implications.

            Assignee:
            dave.cuthbert@mongodb.com Dave Cuthbert (Inactive)
            Reporter:
            backlog-server-pm Backlog - Core Eng Program Management Team
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              2 years, 5 weeks, 3 days ago