Post

Cloudflare Workers and MongoDB

Cloudflare Workers and MongoDB

Previously I wrote about Why Cloudflare Workers Don’t Work With MongoDB, but since then the Cloudflare team has done some great work to add support for the missing Node.js features MongoDB’s driver required to operate successfully from Cloudflare Workers:

Were these changes sufficient to make Cloudflare Workers and MongoDB Atlas work together? Let’s revisit the example from the previous blog post to verify:

Sample Application

Once again, we’ll test the latest iteration of Cloudflare Workers by customizing the compatibility flags and compatibility date in our configuration:

wrangler.toml

1
2
3
4
5
6
7
name = "mongodb-cloudflare-example"
main = "src/worker.ts"
compatibility_flags = ["nodejs_compat_v2"]
compatibility_date = "2025-03-20"

[vars]
MONGODB_URI = "mongodb+srv://..."

src/worker.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { MongoClient } from 'mongodb';

export interface Env {
  MONGODB_URI: string;
}

let client = null;

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    client ??= new MongoClient(env.MONGODB_URI, {
      maxPoolSize: 1, minPoolSize: 0,
      serverSelectionTimeoutMS: 5000,
    });

    const db = client.db('test');
    const coll = db.collection('test_workers');

    await coll.drop().catch(() => null);
    await coll.insertOne({ a: new Date() });

    const result = await coll.findOne({});
    return Response.json(result);
  },
} satisfies ExportedHandler;

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "name": "mongodb-cloudflare-example",
  "version": "0.0.0",
  "type": "module",
  "private": true,
  "scripts": {
    "start": "wrangler dev"
  },
  "dependencies": {
    "mongodb": "^6.15.0"
  },
  "devDependencies": {
    "wrangler": "^4.4.0"
  }
}

To test the code above you would need to do the following:

1
2
npm install
npm start

This will launch Wrangler, the CLI for working with Cloudflare Workers.

Evaluation

Now that our worker is running locally, it should be accessible via http://localhost:8787. The worker logic we wrote in wrangler.ts should perform the following actions:

  • connect to the MongoDB cluster via the connection string configured in the MONGODB_URI environment variable (setup in wrangler.toml)
  • drop the test_workers collection in the test database
  • insert a new document in the test_workers collection
  • retrieve the inserted document from the test_workers collection
  • return a JSON representation of that document

Let’s try it out and see what happens:

1
2
$ curl localhost:8787
{"_id":"67e2e4bb377816f2a78db326","a":"2025-03-25T17:15:39.957Z"}

SUCCESS!

We were able to connect to our cluster (using SCRAM authentication) to interact with our data via the MongoDB Node.js driver from a Worker.

Conclusion

Excitement for Cloudflare Workers support for MongoDB has been around in the MongoDB developer forums since May, 2023. As the Product Manager for MongoDB’s JavaScript developer experience I’ve possibly been making more noise about this issue than most - including the initial blog post. I shared this with the Cloudflare team in a bug report on the workers-sdk, which ultimately triggered a discussion on the workerd project that may have prompted the ensuing engineering effort.

I really look forward to seeing the amazing opportunities this integration can unlock for MongoDB developers that want to take advantage of the power and flexibility of Cloudflare Workers!

This post is licensed under CC BY 4.0 by the author.