I have a test environment, using JUnit 5, where I start up MongoDB in a Docker container. This is done in the @BeforeAll function of my test suite. I have a test for connecting to MongoDB, writing and reading a document, and it works fine.
After I start up the Docker container, then I start my Node.js service in the same @BeforeAll code, and I pass the Mongo URI, such as mongodb://localhost:55021, via a file in the file system. I have verified that my Node.js service gets the URI correctly.
However, in my Node.js service, when I try to connect to MongoDB I get
Cannot connect to MongoDB at mongodb://localhost:55021
MongoServerSelectionError: getaddrinfo ENOTFOUND 944ef754a755
at Timeout.<anonymous> (/Users/ekolotyluk/dev/crypto/customer-js/node_modules/mongodb/lib/sdam/topology.js:310:38)
at listOnTimeout (internal/timers.js:554:17)
at process.processTimers (internal/timers.js:497:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map { '944ef754a755:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'docker-rs',
maxSetVersion: 1,
maxElectionId: new ObjectId("7fffffff0000000000000001"),
commonWireVersion: 8,
logicalSessionTimeoutMinutes: undefined
}
}
Is there some magical difference in MongoDB URIs between the Kotlin Client API and the Node.js API? Do the Java/Kotlin Mongo drivers differ that much from the Node.js drivers?
My Node.js code looks something like
const { MongoClient } = require("mongodb")
const mongoUri = getMongoUri()
const mongoClient = new MongoClient(mongoUri)
async function mongoConnect() {
try {
await mongoClient.connect()
} catch(error) {
console.error(`Cannot connect to MongoDB at ${mongoUri}\n`)
console.error(error)
}
}
It would be nice if the Node.js Mongo driver had better diagnostics. Is there a way to force the driver to provide better diagnostics?
I suspect this may be related to https://baraksaidoff.medium.com/fixing-mongoserverselectionerror-while-connecting-mongodb-with-node-js-213aaf0bf596
However, in my environment, I don't know how to access the MongoDB whitelist programmatically.
In my Node.js application the only IP Address known seems to be 192.168.0.25 but in my Kotlin code 127.0.0.1 is also know. I suspect that 127.0.0.1 is on the whitelist, but 192.168.0.25 is not... ?