Skip to content

Sharding Collection Discovery for MongoDB 6.0+ Compatibility and RBAC Stability#185

Open
jaychakra wants to merge 2 commits into
mongodb:masterfrom
jaychakra:patch-1
Open

Sharding Collection Discovery for MongoDB 6.0+ Compatibility and RBAC Stability#185
jaychakra wants to merge 2 commits into
mongodb:masterfrom
jaychakra:patch-1

Conversation

@jaychakra
Copy link
Copy Markdown

@jaychakra jaychakra commented May 15, 2026

Summary

This PR introduces two critical improvements to getMongoData.js:

  1. MongoDB 6.0+ Support: Fixes missing sharding metadata caused by architectural changes in modern MongoDB versions.
  2. RBAC Hardening: Improves script stability by skipping internal administrative collections that often trigger "Unauthorized" errors.

Feature 1: Fix Sharding Collection Discovery for MongoDB 6.0+

Problem

In MongoDB 6.0+, the partitioned field was removed from the config.databases collection. The legacy logic used if (db.partitioned) to gate collection discovery. Since this field is now undefined, the script incorrectly skips sharding information for modern clusters, leading to empty reports.

Solution

Updated the conditional check to if (db.partitioned !== false). This remains inclusive of undefined (modern) and true (legacy) while still respecting explicit false flags from older versions.

Compatibility Matrix

MongoDB Version partitioned value db.partitioned !== false Result
Legacy (< 6.0) true true Success
Legacy (< 6.0) false false Skip
Modern (6.0+) undefined true Success (Fixed)

Feature 2: Harden Collection Discovery Against Authorization Errors

Problem

When running with restricted RBAC (Role-Based Access Control), the script often encounters "Unauthorized" errors when trying to access internal logs (e.g., local.startup_log) or replica set metadata. These errors can cause the script to terminate prematurely or produce noisy, invalid JSON output.

Solution

Expanded the collection exclusion list in printDataInfo. In addition to system.*, the script now explicitly skips:

  • replicaset*
  • startup_log*
  • replset*

Code Changes Summary

File: getMongoData.js

// 1. Sharding Fix
- if (db.partitioned) {
+ if (db.partitioned !== false) {

// 2. Authorization Hardening
- if (!name.startsWith("system.")) {
+ if (
+     !name.startsWith("system.") &&
+     !name.startsWith("replicaset") &&
+     !name.startsWith("startup_log") &&
+     !name.startsWith("replset")
+ ) {

Verification

  • Validated against MongoDB 8.0.23 where the partitioned field is absent.
  • Confirmed successful completion RS and Sharded environments with auth enabled

@jaychakra jaychakra changed the title Fix Sharding Collection Discovery for MongoDB 6.0+ Sharding Collection Discovery for MongoDB 6.0+ Compatibility and RBAC Stability May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant