Skip to content

fix(plugin-mongodb): parse db.getCollection and align exported columns (#1971) - #1973

Merged
datlechin merged 1 commit into
mainfrom
fix/1971-mongodb-export-getcollection
Jul 27, 2026
Merged

fix(plugin-mongodb): parse db.getCollection and align exported columns (#1971)#1973
datlechin merged 1 commit into
mainfrom
fix/1971-mongodb-export-getcollection

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #1971.

The bug

Exporting a MongoDB collection fails for every format with Unsupported MongoDB method: getCollection, before any data is read.

MongoShellParser.parseDbExpression splits a statement at the last dot before the first (. For db.getCollection("users").find({}) there is no dot before getCollection(, so the parser routes it to parseDbLevelMethod, which treats getCollection as a database method and throws. The string comes from MongoDBPluginDriver.defaultExportQuery, so the export dies before libmongoc is ever called.

The generator was right and the parser was wrong. db.getCollection() is the documented mongosh accessor, and the only way to reach a collection whose name has dots or spaces, starts with a digit, or matches a database method such as stats. Studio 3T, NoSQLBooster, and DataGrip all generate it.

The fix

Parser (MongoShellParser). Adding a case to the database-method switch would not have worked, since parseDbLevelMethod discards the remainder and .find({}) would have vanished. The real defect was that the parser modelled collection references ad hoc in two disconnected places with no room for a third. There is now one string-literal reader (double quotes, single quotes, backticks), one getCollection accessor path that treats the name as opaque so dots inside it stay part of the name, and one shared chain resolver used by all three accessor forms. db["name"] keeps its behaviour and gains backtick support, and its hand-rolled escape loop is gone.

db.getSiblingDB(...) still throws an unsupported-method error on purpose. A query always runs against the connected database, so accepting a cross-database accessor would silently read from the wrong one.

Export query (MongoDBQueryBuilder, MongoDBPluginDriver). defaultExportQuery hand-rolled a third accessor form with no escaping, so a collection name containing a quote or a backslash produced a malformed query. It now calls the new buildExportQuery(collection:), so browse, filter, count, and export all render names through the same accessor.

Streamed columns (MongoDBConnection+SyncHelpers, new MongoStreamProjection). With the parse fixed, export still wrote bad files. The header was built from the first document alone, then columns grew for later documents without being re-announced: CSV rows ended up wider than their header, and JSON dropped those fields from every document. Since MongoDB collections are schemaless, that hits any collection with an optional field. Streaming now buffers a bounded sample, derives the column union from it, emits the header once, flushes the buffered rows, and projects everything after that onto the fixed header. Columns never grow after the header, so rows always match it.

Rollout

The parser lives in TableProPluginKit, which is embedded only in the app target. The MongoDB plugin links it dynamically and carries no copy, so the parse fix repairs export for every already-installed MongoDB plugin as soon as the app ships. No ABI bump, no plugin re-release needed for that half. The two plugin-side changes reach users with the next plugin-mongodb-v* release.

Tests

  • 14 parser cases: all three quote styles, bare reference, chained find/sort/skip/limit, dotted name kept whole, a name colliding with a database method, an escaped quote, a write method, whitespace before the argument list, three error cases, and getSiblingDB staying unsupported.
  • 4 query-builder cases, including a round trip that parses buildExportQuery output back to a find on the same collection. That is the test that would have caught this.
  • 7 projection cases covering the alignment guarantee: missing fields become null, extra fields never widen a row, and every row matches the header width.

swiftlint lint --strict is clean on the changed files. The two violations it reports reproduce on the unmodified files at main and sit in directories the repo config does not lint.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@mintlify

mintlify Bot commented Jul 27, 2026

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
TablePro 🟢 Ready View Preview Jul 27, 2026, 12:13 PM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@datlechin
datlechin merged commit 0535328 into main Jul 27, 2026
3 checks passed
@datlechin
datlechin deleted the fix/1971-mongodb-export-getcollection branch July 27, 2026 12:14
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.

Export issues when working into MongoDB connection

1 participant