don’t think its possible to force developers, plus it does seem like a infrastructure issue as cloud flare workers isn’t a runtime like Node, its a worker thread, similar to the one in the browser.
Hey @A_B7,
We can’t force Cloudflare to do anything, but based on mongodb · cloudflare/workerd · Discussion #2721 · GitHub it sounds like there might be some movement on this in January.
More information will be shared as it becomes available, but I’d recommend following that discussion thread as well for the time being.
HELLO EVERYBODY! I got it to work!
I was able to get the mongodb driver to work in cloudflare workers using module-aliases and TCP sockets!
Its a very minimal implementation and I would really be very grateful for any contributions to the repository
I was able to achieve this by using module-aliases to polyfill the net.createConnection
and tls.connect
functions and make it return a mock Socket that utilizes cloudflares TCP sockets
The repository can be found here
James you are the BEST! I just tried it out and it appears to work. I only tried on a single call to the database but it worked. I’m using it within a React-Router7 app deployed on workers.
There remains one small issue, not with the polyfill, but with the workers/mongodb Atlas integration. The problem is that network access to your cluster is only possible via IPv4, and CF-workers all use the the same IPv6 address, specifically 2a06:98c0:3600::103. As I see it, one needs to allow open access to the cluster, which really isn’t great for security.
npm error 404 Not Found - GET https://registry.npmjs.org/cf-mongodb-polyfills - Not found
Going to download it right now and manually try it
…or can you…
Not sure why, but the link in James’ post is now dead. But here are the links:
Repo: https://github.com/jchoi2x/cf-mongodb-polyfills
NPM(github): Package cf-tcp-mock · GitHub
And there seems to be an issue Github, pages are really slow to load.
it seems to be working. but getting some weird cross promise resolution issues connecting and disconnecting db in the index.ts
any suggestion on how to handle connection and disconnection?
what’s my reward?
I am unable to get GitHub - jchoi2x/cf-mongodb-polyfills: Allows using of mongodb npm package in cloudflare workers environment by polyfilling missing modules working with Atlas. I wonder if it’s because of the IPv6 address access problems between cloudflare & Atlas. Not sure how to fix that. Opened an issue here: BSONOffsetError error when trying to load from Atlas · Issue #4 · jchoi2x/cf-mongodb-polyfills · GitHub
you dont need that anymore.
Awesome, is this/can this be an official release that workers now support Mongodb drivers? The community solve the data api deprecation!
It is very exciting that Cloudflare is making progress on this. I haven’t had a chance to test these changes yet, but given this is just adding support for node:net
, to connect to Atlas they’ll likely still need to implement node:tls
.
Hello Ben,
I am also getting the same error BSONOffsetError
but I am only getting it intermittently. I am certain that it is not related to the polyfill. As far as I can tell, it is likely due to a memory limit, or some other limit. I’m getting the error on M0 free cluster, when doing $vectorSearch
. I can prevent it from occurring by setting a low limit, eg: 1 or 2 docs, as opposed to 10 or more. I don’t see any errors in my MongoAtlas dashboard, nor do any appear in Cloudflare.
If I run my app in locally in dev mode (not using wrangler) I don’t get the error, but with Wrangler, in preview or deployed, I do get the errors. So not really sure what is causing the issue, but it is frustrating.
I should probably file an issue, but I’m not sure where, any suggestion?
Just wanted to close the loop with an update blog post: Cloudflare Workers and MongoDB | ALEX BEVILACQUA
The latest release of Cloudflare Workers should support connectivity and interaction with MongoDB Atlas. I haven’t done a full barrage of testing, but I can confirm the limited tests I ran are successful
Thank for the test. Unfortunately i’m still getting a
“MongoServerSelectionError: socket.once is not a function”
because @alexbevi is, according to his own blog post, fully responsible for this fix release, you should hold him accountable for any issues
pretty sure that’s not what I’m implying (though I might have made the most noise) … but based on the error it does’t seem the workerd
was updated to pull in the latest dependencies.
Maybe, though i followed your nice blog post
wrangler.toml
compatibility_flags = [“nodejs_compat_v2”]
compatibility_date = “2025-03-20”
pages_build_output_dir = “.svelte-kit/cloudflare”
I will have a look again i might have missed something (for now i’m staying on Vercel)
same here! I’m still getting a “MongoServerSelectionError: socket.once is not a function” in sveltekit project
New & Unread Topics
Topic | Replies | Views | Activity |
---|---|---|---|
we are facing Data loss issue while Inserting From Server | 0 | 490 | May 2024 |
Atlas SQL with Power BI - publishing to Report Server | 0 | 494 | May 2024 |
Kafka Connector | 0 | 50 | Oct 2024 |
Connect ODI to MongoDB 8.0.3 | 2 | 104 | Nov 2024 |
[mongodb - ODBC - PowerBi] retrieval of collections is really slow or powerbi container closed | 0 | 44 | Jan 7 |