Connecting to a MongoDB (with ReplicaSet) running in Docker worked in 5.13.9 but fails in 6.0.8
docker-compose.yml for the database:
This is the docker-compose.yml file for the Database:
version: '3.9'
services:
loggerdb:
image: mongo
container_name: loggerdb
command: ["--replSet", "rs0", "--bind_ip_all", ]
restart: always
ports:
- "27017:27017"
volumes:
- ./data:/data/db
environment:
MONGO_INITDB_DATABASE: logger
networks:
default:
external: true
name: logger-network
As you see, there is no Authentication and the database is listening to 27017 on localhost.
The database is running in the Docker Container:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b42adb1e26ec 0bcbeb494bed "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp loggerdb
Code to connect (5.13.9)
The code is in Typescript, and for the version 5.13.9 Mongoose I use the following:
mongoose.connect('mongodb://localhost:27017/logger', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
app.listen(PORT, () => {
console.log(`Listening on ${PORT} for Log Messages`);
});
});
It is necessary to specify useNewUrlParser and useUnifiedTopology here, otherwise you'll get an error.
The result is that this is working correctly. The program is connecting to the database and I can write stuff to it.
Code to connect (6.0.8)
The code for 6.0.8 is slightly different:
mongoose.connect('mongodb://localhost:27017/logger?replicaSet=rs0')
.then(() => {
app.listen(PORT, () => {
console.log(`Listening on ${PORT} for Log Messages`);
});
});
The useNewUrlParser and useUnifiedTopology are obsolete now, and the docs show you need to specify the ReplicaSet as a param.
The result of connecting with this code to 6.0.8 Mongoose is as follows:
[INFO] 14:33:33 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.3.5)
MongooseServerSelectionError: getaddrinfo ENOTFOUND b42adb1e26ec
[[ Removed the StackTrace ]]
[ERROR] 14:34:05 MongooseServerSelectionError: getaddrinfo ENOTFOUND b42adb1e26ec
Question
What is the correct to connect to a Mongo Standalone ReplicaSet, running in Docker, with Mongoose 6.0.8 ?