Mongoose Server selection Error

I have 4 VM where Docker containers are running for Node js services which are connecting with MongoDB which are running in a docker container on other 3 VM. when we deploy the service sometimes the only service from 1st VM get connected to mongo sometimes other VM NodeJs service shows the server selection error Shown in the image and all ports are allowed to connect internally within All 7 VM Mongo Version 4.2.8 Node j


s Version 14.2.0 Mongoose Version 5.9.6

me also can you help me in this error

this worked for me:

within your mongoDB atlas…
in Network Access… edit IP access list
switch it to “allow access from anywhere”

It did worked for you because you are connecting to an Atlas cluster. The other person is not using Atlas.

replace local host with 127.0.0.1 it work every time

1 Like

thank you so much !!

Hi all,

This kind of error has many root causes but its occurring “sometimes” might be related to the number of connections opened to DB servers. the default number is 64K (65536) and can be changed in the config file under net.maxIncomingConnections

Configuration File Options — MongoDB Manual

If your app does not use a singleton client or does not close connections after querying, at some point total connections to the database will accumulate and reach a point where it won’t accept new connections anymore until old ones are closed/expired.

for example, if the driver opens 100 connections for each request by default, then only about 655 of them will be served and the 656th connection (and over) will be refused as it can serve only 36 more (for default).