Getting old shard keys before resharding

Hello,

Querying config.collections returns data that includes the current shard keys of a collection. Resharding a collection with a new shard key changes the corresponding entry in config.collections. Is there a way to know the old shard keys of the collection i.e. the shard keys before resharding was performed?

Entry in config.collections before resharding

{
  "_id": "src.data",
  "lastmodEpoch": {
    "$oid": "64340cfd942da8390e3ca588"
  },
  "lastmod": {
    "$date": "2023-04-10T13:19:57.427Z"
  },
  "timestamp": {
    "$timestamp": {
      "t": 1681132797,
      "i": 6
    }
  },
  "uuid": {
    "$binary": {
      "base64": "HLO3ebKGT4ONcEFkdwWRqw==",
      "subType": "04"
    }
  },
  "key": {
    "student.name": 1,
    "city": "hashed"
  },
  "unique": false,
  "chunksAlreadySplitForDowngrade": false,
  "noBalance": false
}

Entry in config.collections after resharding

{
  "_id": "src.data",
  "lastmodEpoch": {
    "$oid": "643674a8ccaf8db708026956"
  },
  "lastmod": {
    "$date": "2023-04-12T09:06:48.649Z"
  },
  "timestamp": {
    "$timestamp": {
      "t": 1681290408,
      "i": 3
    }
  },
  "uuid": {
    "$binary": {
      "base64": "YzCh1albRzaJ8i3vtCoDLg==",
      "subType": "04"
    }
  },
  "key": {
    "student.name": 1,
    "city": 1,
    "order_id": "hashed"
  },
  "unique": false,
  "chunksAlreadySplitForDowngrade": false,
  "noBalance": false
}

I can see that config.collections stores the timestamp at which resharding was performed. But does MongoDB store the historic data on shard keys? If yes, where do we find it?

Hello @Ajay_Mathias1 ,

Welcome to The MongoDB Community Forums! :wave:

You can try to have a look at config.changelog collection in the config server. As this collection is capped in size, thus if you had a lot of DDL operation in the cluster since you originally sharded the collection there is the possibility that the original shard collection event was already deleted from config.changelog

Note: Internal MongoDB Metadata

The config database is internal: applications and administrators should not modify or depend upon its content in the course of normal operation.

Apart from that, MongoDB does not store historical data on shard keys. Once a collection is resharded with a new shard key, the old shard key information is removed and cannot be retrieved.

For future, If you need to retain historical information on shard keys, you can include the old shard key information as part of the data stored in the collection itself, such as in a separate field or document, to ensure it is not lost when the collection is resharded.

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.