-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MongoDB Node.js Driver doesn't work when connecting to MongoDB Atlas from Deno #16633
Comments
The same error pops up on Linux x86_64 with Deno 1.28.1 |
Same issue! |
Same issue here with Deno v1.28.2 |
Same on Deno 1.30 and npm MongoDB 5.0.0 |
Note the MongoDB Node.js team has filed NODE-5042 which may address the "MongoAPIError: Server record does not share hostname with parent URI" portion of this issue. |
is there any solution for this? |
Interesting development. Using a DNS Seed List in the URI appears to work correctly in the latest Deno:
// file: main.ts
import { MongoClient } from 'npm:mongodb' // updated for Deno
const URI =
"mongodb://<user>:<pass>@cluster0-shard-00-00.abcde.mongodb.net:27017,cluster0-shard-00-01.abcde.mongodb.net:27017,cluster0-shard-00-02.abcde.mongodb.net:27017/?ssl=true&replicaSet=atlas-122v8z-shard-0&authSource=admin&retryWrites=true&w=majority"
const client = new MongoClient(URI)
async function main() {
console.log('connecting')
await client.connect()
console.log('connected')
const collection = client.db('test_db').collection('test_collection')
await collection.deleteMany({})
await collection.insertOne({ a: 2.3 })
var results = await collection.count({ a: 2.3 })
console.log(`found ${results}`)
Deno.exit(1)
}
main()
|
This issue still remains on deno 1.31.3.
Connection code:
|
Damn! I hope this is fixed soon. It is very annoying not being able to work with mongo and Deno, and MongoDB Deno native library is abandoned and I think is does not support MongoDB v6 |
With the "mongodb+srv" connection string |
I think I have figured out the source of this problem. When connecting with the To do that, it calls Here's the code I used to test this: import dns from "node:dns";
console.log(await dns.promises.resolveSrv("_mongodb._tcp.cluster.1abcd.mongodb.net")) And here are the outputs when running in Deno: [
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-01.1abcd.mongodb.net."
},
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-02.1abcd.mongodb.net."
},
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-00.1abcd.mongodb.net."
}
] in node: [
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-01.1abcd.mongodb.net"
},
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-02.1abcd.mongodb.net"
},
{
priority: 0,
weight: 0,
port: 27017,
name: "cluster-shard-00-00.1abcd.mongodb.net"
}
] I edited all the hostnames displayed there to hide some info but that's what it should look like. |
calling |
@hazelnutcloud from a MongoDB Driver POV a bug for this was reported previously (see NODE-5042) which we're hoping to get sorted out soon. It's really helpful to see further confirmation that resolving this bug should improve the Deno experience using MongoDB! |
Thanks for investigating @hazelnutcloud, @crowlKats is looking into this issue and it should be fixed in the coming days. |
Just chiming in with an update. The ticket over at MongoDB's Jira is marked as done with fix version v5.3.0. |
@eikooc you beat me to it :) The MongoDB Node Driver team addressed this with mongodb/node-mongodb-native@051b861, which will be included in the next release of the driver. I'll close this issue out once the release is made :) |
FYI https://github.com/mongodb/node-mongodb-native/releases/tag/v5.3.0 has been released, which includes the fixes to SRV resolution that were affecting Deno. There may still be an opportunity to address this behavior in Deno directly, but for now the MongoDB driver should work :) |
It now started connecting to MongoDB with
|
@topperspal I experience the same thing |
I am experiencing the same issue as @topperspal on Perhaps worth noting that |
@TillaTheHun0 if you can create a minimal reproduction then there's this issue where you can post it #19078 I haven't been able to create a succesful minimal production showing the error. |
@eikooc I posted a repo with steps to reproduce. I was only able to replicate with an Atlas Serverless Cluster, which seems correlated, but can't say for sure without digging deeper. |
I've noticed that this happens every time the MongoDB cluster scales up or down so the connection is cut. |
I believe this has been fixed in v1.37 #20314. |
Thanks for checking @eikooc! Let's close the issue then. |
I jus tried on local an deploy and seems to be working with MongoDB Atlas serverless deno v1.37 import { MongoClient } from "npm:mongodb"
import { ulid } from "https://deno.land/std@0.202.0/ulid/mod.ts";
const client = new MongoClient("<url>")
client.connect();
Deno.serve(async () => {
const data = await client.db("deno").collection("deno").insertOne({
name: ulid()
})
return new Response(JSON.stringify({
data
}), {
headers: { "Content-Type": "application/json" },
})
}) |
Does anyone experience this error yet? It happens so random i can't reproduce it locally, but my serverless service sometimes crashes because of this UPD: according to my atlas metrics, it happens on server restarts, so nevermind |
@ryvwel that's just a socket error, so "why" the connection was closed would need investigation (it's likely not a Deno, or even MongoDB issue). Note that MongoDB's drivers support automatic retryability of reads and writes so it's possible the associated operation may not have even failed (though you may see a logged error). This is likely a transient network error, so handling the uncaught exception and allowing the driver's connection pool to heal itself should allow you to continue normal operations. |
@ryvwel @topperspal Further links to others in that issue |
We believe this is now fixed with #21951. Please try with Deno v1.45.4 and let us know if the problem persists. |
Potentially related to issue #15824, but the
mongodb
driver cannot be used directly via Deno when targeting a MongoDB Atlas cluster. This was reported at NODE-4782 however it doesn't appear to be an issue with the Node driver itself.For example, when running the preceding using
npx ts-node main.ts
the script will connect to Atlas, authenticate/authorize, clear the collection, insert a new doc and execute a count.To make this run with Deno the script was updated as follows:
When the above script variation is run using
deno run --unstable --allow-all main.ts
it will fail with the following error as it doesn't appear to be able to resolve a DNS Seed List Connection String:Changing the
URI
constant to use a Standard Connection String ofmongodb://user:pass@cluster0-shard-00-00.abcde.mongodb.net:27017,cluster0-shard-00-01.abcde.mongodb.net:27017,cluster0-shard-00-02.abcde.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority
and re-running results in the operation just timing out:Note this was tested with:
Steps to Reproduce
mongodb://
version instead ofmongodb+srv://
)URI
constant with your connection stringdeno run --unstable --allow-all main.ts
The text was updated successfully, but these errors were encountered: