I explained the problem in detail on Stack Overflow, but I think this behaviour is a bug and should be reported here as well.
In brief, when MongoClient.connect fails with the error:
MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.18.0.2:27017]
It rejects the promise, but it continues to retry to connect and when it's successful it's not reported to the promise (or callback), which means that I cannot use the connection, but it's there and continues to exist until the application is closed.
Currently when this happens I retry by calling MongoClient.connect again and for each failure I get a new connection, but at some point I get a connection that I can actually use. At this point the app might have hundreds of connections to the database, but only one usable. Besides terminating the app, I don't have any way of closing the unused connections. The database logs clearly shows that they are opened, but never closed, until the app is terminated.
I don't think this behaviour is useful and I didn't find anything about this in the documentation, so I'm assuming that it's unintended and hence a bug. If there is any recommended workaround I would be very interested, as I'm not very happy with my current workaround.