I have a NodeJS program using mongoose 5.13.17 and I connect using this statement:
connect = () => mongoose.connect(url, options);
connect();
where url is mongodb://host.docker.internal:37017/urbo-test and options is
{
useNewUrlParser: true,
useCreateIndex: true,
keepAlive: true,
reconnectTries: 10
}
However, I get an error like this:
MongoNetworkError: failed to connect to server [host.docker.internal:37017] on first connect [MongoNetworkTimeoutError: connection timed out
at connectionFailureError (/workspaces/urbo/node_modules/mongodb/lib/core/connection/connect.js:362:14)
at Socket.<anonymous> (/workspaces/urbo/node_modules/mongodb/lib/core/connection/connect.js:330:16)
at Object.onceWrapper (node:events:627:28)
at Socket.emit (node:events:513:28)
at Socket._onTimeout (node:net:570:8)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)]
As you could have figured by the URL, I'm running my program inside a docker container. My first hypothesis was some kind of network connection problem, but if I try with the mongo shell in the very same container, I can connect without problems:
$ mongosh mongodb://host.docker.internal:37017/urbo-test
Current Mongosh Log ID: 646b7f8ddfa2f439faf7df83
Connecting to: mongodb://host.docker.internal:37017/urbo-test?directConnection=true&appName=mongosh+1.9.0
Using MongoDB: 4.4.21
Using Mongosh: 1.9.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2023-05-22T04:32:52.491+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-05-22T04:32:54.727+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
urbo-test>
As additional information, I'm getting this warning when I run my program (just before the error message):
(node:20834) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use `node --trace-warnings ...` to show where the warning was created)
Any idea of what can be the reason of the problem, please? Thanks in advance!
EDIT: I have modified the options object so now it is:
{
useNewUrlParser: true,
useCreateIndex: true,
keepAlive: true,
useUnifiedTopology: true
}
This makes deprecation warning to disappear but I'm still getting a connection error (however, the text is slightly different):
MongooseServerSelectionError: connection timed out
(Adding useUnifiedTopology: true is not enough; reconnectTries has to be removed or a new warning about `reconnectTries is incompatible with the unified topology is raised)
EDIT2: as additional information, this is my Nodejs version:
$ node --version
v18.16.0