Skip to content

Show shard key in collection tooltip 🔑 #661

@tnaum-ms

Description

@tnaum-ms

Problem

Sharded collections in the tree view don't display their shard key anywhere visible. Users must use external tools (Azure Portal, shell) to check which field(s) a collection is sharded on. This information is important for understanding query performance and data distribution.

The raw MongoDB listCollections() response includes an options field that can contain shard key information, but the current CollectionItemModel interface only preserves name and type, discarding this data.

Expected Behavior

When a collection has a shard key, it should be displayed in the collection's tooltip:

### my-collection

`Collection`

---

**Database:** my-database
**Documents:** 5.2k
**Shard Key:** { userId: "hashed" }

Development Hints

Since this is a good first issue, here are some hints on where to start:

Step 1: Extend the model in src/documentdb/ClustersClient.ts

The CollectionItemModel interface currently only captures name and type:

export interface CollectionItemModel {
    name: string;
    type?: string;
}

Add an optional options field (or specifically a shardKey field) to preserve the shard key from the MongoDB response. The raw listCollections() response already contains this data, but it gets discarded during the type assignment:

const rawCollections = await this._mongoClient.db(databaseName).listCollections().toArray();
const collections: CollectionItemModel[] = rawCollections; // options field is lost here

Step 2: Update the tooltip in src/tree/documentdb/CollectionItem.ts

In buildTooltip(), add a section for the shard key when it exists. Format the key object as a readable string (e.g., { userId: "hashed" } or { region: 1, date: 1 }).

Notes:

  • Not all collections are sharded. The shard key section should only appear when the data is present.
  • The shard key is a key-value object where values indicate the sharding strategy (e.g., 1 for range, "hashed" for hash-based).
  • The listCollections() response format may vary. Test with both sharded and non-sharded collections.
  • Some DocumentDB configurations may not return shard key info at all. Handle missing data gracefully.

Files to Modify

File Change
src/documentdb/ClustersClient.ts Extend CollectionItemModel to include shard key data
src/tree/documentdb/CollectionItem.ts Add shard key section to buildTooltip()

Acceptance Criteria

  • CollectionItemModel preserves shard key data from the listCollections() response
  • Sharded collections show the shard key in the tooltip
  • Non-sharded collections are unaffected (no empty shard key section)
  • Shard key is formatted readably (e.g., { field: 1 } or { field: "hashed" })
  • No changes to tree item label, description, or icon

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions