Skip to content

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

Merged
niccottrell merged 4 commits into
mongodb:masterfrom
jaychakra:patch-1
May 26, 2026
Merged

Sharding Collection Discovery for MongoDB 6.0+ Compatibility and RBAC Stability#185
niccottrell merged 4 commits into
mongodb:masterfrom
jaychakra:patch-1

Conversation

@jaychakra
Copy link
Copy Markdown
Contributor

@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.")) {

+ // Always filter out "system." collections across all databases.
+ if (name.startsWith("system.")) {
+    return;
+ }

+ // Filter MongoDB-internal replication metadata collections, but ONLY
+ // in the "local" database, to avoid dropping legitimate user
+ // collections (e.g., app.replsetEvents, app.startup_log_archive)
+ // that happen to share a similar name in user databases.
+ if (mydb.name === "local") {
+     if (name === "startup_log" || name.startsWith("replset.")) {
+        return;
+    }
+}

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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the getMongoData Mongo shell script to better report sharding metadata on MongoDB 6.0+ clusters and to reduce failures/noise under restricted RBAC by skipping certain internal collections during data collection.

Changes:

  • Adjust sharded database detection to treat partitioned: undefined (MongoDB 6.0+) as eligible for sharding collection discovery.
  • Expand collection filtering in printDataInfo to skip additional internal collections that commonly trigger authorization errors.
  • Bump script version from 4.1.1 to 4.1.2.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread getMongoData/getMongoData.js Outdated
Refactor collection filtering logic to simplify checks for system collections and replication metadata.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment on lines +567 to +573
// Filter MongoDB-internal replication metadata collections, but ONLY
// in the "local" database, to avoid dropping legitimate user
// collections (e.g., app.replsetEvents, app.startup_log_archive)
// that happen to share a similar name in user databases.
if (mydb.name === "local") {
if (name === "startup_log" || name.startsWith("replset.")) {
return;
@niccottrell niccottrell merged commit 1f6c873 into mongodb:master May 26, 2026
1 check passed
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.

3 participants