Skip to content

[Workers] Add MongoDB integration docs#31382

Open
MattieTK wants to merge 9 commits into
productionfrom
feat/mongodb-docs
Open

[Workers] Add MongoDB integration docs#31382
MattieTK wants to merge 9 commits into
productionfrom
feat/mongodb-docs

Conversation

@MattieTK

@MattieTK MattieTK commented Jun 10, 2026

Copy link
Copy Markdown
Member

Summary

Adds documentation for connecting Cloudflare Workers to MongoDB using the native Node.js driver (mongodb >= 6.15.0) and Mongoose. We occasionally get requests in discord for information on how to do this since we don't support these drivers in Hyperdrive, and setting up database connections on a request-basis is inefficient.

Three changes:

  1. New integration page at /workers/databases/third-party-integrations/mongodb/ – covers both a direct connection (simple, ~300ms overhead per request) and a Durable Object approach (recommended, persists connections for ~35ms warm queries). Also documents the Mongoose package.json exports patch required for workerd compatibility.

  2. Table row added to /workers/databases/connecting-to-databases/ under the serverless databases section, listing MongoDB with the mongodb/mongoose drivers and TCP Socket as the connection method.

  3. New tutorial at /workers/tutorials/connect-to-mongodb-atlas/ – a step-by-step guide that creates a Worker, connects to MongoDB, queries a sample collection, then upgrades to a Durable Object for lower latency.

Relevant upstream context:

Documentation checklist

  • Is there a changelog entry (guidelines)? If you don't add one for something awesome and new (however small) — how will our customers find out? Changelogs are automatically posted to RSS feeds, the Discord, and X.
  • The change adheres to the documentation style guide.
  • If a larger change - such as adding a new page- an issue has been opened in relation to any incorrect or out of date information that this PR fixes.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/workers/ @cloudflare/workers-docs, @GregBrimble, @irvinebroque, @mikenomitch, @korinne, @WalshyDev, @cloudflare/deploy-config, @cloudflare/product-owners, @cloudflare/wrangler, @MattieTK, @cloudflare/dev-plat-leads, @vy-ton

@ask-bonk ask-bonk Bot added content:new Request for new/missing content documentation Documentation edits labels Jun 10, 2026

@ask-bonk ask-bonk Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

3 issues need fixing before merge.

HIGH — Workers TypeScript examples must use TypeScriptExample (not bare ts/typescript fences). I suggested the most prominent blocks; please apply the same wrapper to any remaining examples on both pages.

HIGHWranglerConfig blocks must use TOML input and $today for compatibility_date instead of JSONC/hardcoded dates.

MEDIUM — The tutorial output block should use txt output rather than sh output.

Comment thread src/content/docs/workers/databases/third-party-integrations/mongodb.mdx Outdated
Comment thread src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx Outdated
Comment thread src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx Outdated
Comment thread src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx
Comment thread src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx
Comment thread src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx
@ask-bonk

ask-bonk Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review posted on PR #31382.

Labels applied: product:workers, content:new, documentation.

Summary of findings:

Severity Issue Location
HIGH Workers TypeScript examples use bare ts/typescript fences instead of the required TypeScriptExample component. mongodb.mdx (3 blocks), tutorial (3 blocks)
HIGH WranglerConfig blocks use JSONC input and hardcoded compatibility_date instead of TOML + $today. mongodb.mdx, tutorial
MEDIUM Command output block uses sh output instead of txt output. Tutorial step 4

I posted 11 inline suggestions covering the import additions, representative TypeScriptExample wrappers, both WranglerConfig conversions, and the output language fix. The author should apply the same TypeScriptExample wrapper to any smaller typescript snippets I didn't individually flag (e.g., the step 5 import snippets in the tutorial).

No pre-existing issues in unchanged files were flagged. The content is technically accurate and the structure is solid once the component/style-guide violations are resolved.

github run

MattieTK and others added 8 commits June 11, 2026 13:38
…ngodb.mdx

Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
…ngodb.mdx

Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
…ngodb.mdx

Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
…ngodb.mdx

Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
Close TypeScriptExample and WranglerConfig blocks that were missing
fence/tag terminators, and fill in the empty example blocks in the
MongoDB integration page and Atlas tutorial.
@MattieTK MattieTK marked this pull request as ready for review June 23, 2026 10:57
@MattieTK MattieTK requested review from a team as code owners June 23, 2026 10:57
@MattieTK MattieTK requested a review from penalosa June 23, 2026 10:57
@cloudflare-docs-bot

cloudflare-docs-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review

🚨 2 critical, ⚠️ 9 warnings, 💡 3 suggestions found in commit 102e695.

Code Review

This code review is in beta and may not always be helpful — use your judgment.

Critical (2)
File Issue
workers/databases/third-party-integrations/mongodb.mdx line 242 Missing import for DurableObjectState — The Mongoose example's constructor declares state: DurableObjectState, but it only imports { DurableObject } from "cloudflare:workers" (line 227). The DurableObjectState type is not in scope, so the snippet will not compile. Fix: Import DurableObjectState alongside DurableObject from "cloudflare:workers".
workers/databases/third-party-integrations/mongodb.mdx line 244 Repeated mongoose.model registration in constructormongoose.model("Movie", movieSchema, "movies") is called every time the Durable Object is constructed. Mongoose throws OverwriteModelError when a model with the same name is registered more than once in the same isolate, which will happen as Durable Object instances are created/evicted within a Worker isolate. Fix: Define the model once at module scope (e.g. const Movie = mongoose.model("Movie", movieSchema, "movies")) and assign this.Movie = Movie in the constructor, or guard registration with mongoose.models["Movie"] ?? mongoose.model(...).
Warnings (2)
File Issue
workers/tutorials/connect-to-mongodb-atlas.mdx line 141 MongoClient leaked per request — The simple Worker example creates a new MongoClient and connects it on every request, but the client is never closed or disposed before the handler returns. Fix: Wrap the connection logic in a try/finally and call await client.close() before returning, or switch to a single client reused for the isolate lifetime.
workers/databases/third-party-integrations/mongodb.mdx line 64 Unclosed MongoClient connection — The direct-connection example creates a new MongoClient and opens a connection, then returns the response without ever calling client.close(), leaking a TCP/TLS connection for each Worker invocation. Fix: Wrap the query logic in a try/finally block and call await client.close() in the finally to ensure the connection is cleaned up.

Style Guide Review

Warnings (7)
File Issue
workers/tutorials/connect-to-mongodb-atlas.mdx line 92 Use for package manager commands — Bare ```sh code block contains npx wrangler secret put MONGODB_URI Fix: Use `` instead of a bare code fence
workers/tutorials/connect-to-mongodb-atlas.mdx line 269 Use for package manager commands — Bare ```sh code block contains npx wrangler deploy Fix: Use `` instead of a bare code fence
workers/tutorials/connect-to-mongodb-atlas.mdx line 284 Internal links must include trailing slash — Links (/workers/tutorials) and (/workers/databases) are missing trailing slashes Fix: Change to (/workers/tutorials/) and (/workers/databases/)
workers/databases/third-party-integrations/mongodb.mdx line 22 No directional words — Admonition uses 'as shown below' Fix: Replace 'as shown below' with a direct reference to the Durable Object example.
workers/databases/third-party-integrations/mongodb.mdx line 226 Pass Expressive Code options via TypeScriptExample props — Inner ```ts fence has title="src/MongoDBConnection.ts" inside Fix: Remove the inner fence title; the filename is already set on the component prop.
workers/databases/third-party-integrations/mongodb.mdx line 159 Use TypeScriptExample for Workers-style code — Bare ```ts block exports a default Worker fetch handler Fix: Wrap the block in .
workers/databases/third-party-integrations/mongodb.mdx line 205 Use PackageManagers for npx commands — Fenced block contains only 'npx wrangler deploy' Fix: Replace with .
Suggestions (3)
File Issue
workers/tutorials/connect-to-mongodb-atlas.mdx line 124 Avoid navigate to phrasing — Line uses navigate to your cluster Fix: Change to go to your cluster
workers/databases/third-party-integrations/mongodb.mdx line 27 Tabs primary answer first — First tab is 'Direct connection (simple)' while the second tab is 'Durable Object (recommended)' Fix: Place the recommended Durable Object tab first.
workers/databases/third-party-integrations/mongodb.mdx line 264 Standard link phrasing — Next-step bullet uses 'Learn more about Durable Objects' Fix: Change to 'For more information, refer to Durable Objects.'
Commands

Only codeowners can run commands. Post a comment with the command to trigger it.

Command Description
/review Runs a review now. Incremental if a prior review exists, full if not.
/full-review Re-reviews the entire PR diff from scratch, ignoring incremental history. Useful after a rebase, when you want a fresh review, or if the bot gets out of sync and reports issues that no longer exist.
/ignore-review-limit Permanently lifts the 2-review automatic limit for this PR. Future pushes will trigger reviews as normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

content:new Request for new/missing content documentation Documentation edits product:workers Related to Workers product size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants