MongoError: no connection available to server

Hi,

I found very strange issue while reading the document from a collection .
From MongoDB Atlas dashboard I can see number of connections are available , When I tried db.collection(collectionname).countDocuments API then it responds error

MongoError: no connection available to server myclustername--shard-00-02.lp8yd.mongodb.net:27017.

The detailed log is provided below:

This is first time I observed this issue and after this it did not reproduce .

Did anyone face this kind of issue that lot of connections are available and still atlas sends “no connection available error” .

I am using MongoDB Nodejs Driver and detailed logs are here.

at disconnectHandler (/var/task/node_modules/mongodb/lib/core/topologies/server.js:271:14)
    at Server.command (/var/task/node_modules/mongodb/lib/core/topologies/server.js:625:7)
    at CountDocumentsOperation.executeCommand (/var/task/node_modules/mongodb/lib/operations/command_v2.js:95:12)
    at CountDocumentsOperation.execute (/var/task/node_modules/mongodb/lib/operations/aggregate.js:93:11)
    at CountDocumentsOperation.execute (/var/task/node_modules/mongodb/lib/operations/count_documents.js:22:11)
    at /var/task/node_modules/mongodb/lib/operations/execute_operation.js:144:17
    at _selectServer (/var/task/node_modules/mongodb/lib/core/topologies/replset.js:1160:5)
    at ReplSet.selectServer (/var/task/node_modules/mongodb/lib/core/topologies/replset.js:1163:3)
    at ReplSet.selectServer (/var/task/node_modules/mongodb/lib/topologies/topology_base.js:342:32)
    at executeWithServerSelection (/var/task/node_modules/mongodb/lib/operations/execute_operation.js:131:12)

Hi Ajay,

This is very odd indeed: out of curiosity did you create a support case? You were connected to the full replica set rather than an individual node in the cluster right?
-Andrew

Hi Andrew,
I am using Atlas mongodb and I believe by default atlas creates replica set.
For connecting the DB from my application I am using connect API with these below options.
It should connect with full replica set, or is it connecting with any one cluster node ?
MongoClient.connect(“mongodb+srv://UserName:@myclustername.lp8yd.mongodb.net/myFirstDatabase?retryWrites=true&w=majority”, {useNewUrlParser: true, useUnifiedTopology: true},

of course I changed usename , pwd and clustername with the real values.
Regards
Ajay

Hi Ajay,

By default that modern SRV connection string will get you access to the full replica set: SRV allows for each node’s public DNS hostname to be discovered inside the SRV record itself automatically (this is supported by newer generation MongoDB drivers). This makes for a more concise connection string.

Cheers
-Andrew

Hi Andrew,
Thanks for your reply. It is confirmed that I was connected with the full replica set.
Now the issue is when there are plenty of connection counts are available then why mongodb atlas replied "no connection available to server " ?
This is very strange , Please help to know why it misbehaved and how this can be handled.

Regards
Ajay

Hi Ajay, Please open a support case or use the lower-right chat in the UI: that team will be better positioned to help you with troubleshooting.

Cheers
-Andrew

Hi Andrew,
First I contacted the support person by using lower-right chat in the UI.
The support person first says that “It is due to election between nodes”, then I replied him that while going election it does not impact the read operations.

After this he says, yes it should not impact the read operations and finally he said " I am afraid, we are not able to provide a root cause for this matter from within this chat. You may wish to check out our community support resources, where our other MongoDB users like yourself are frequently answering questions "

After reading his answer I was disappointed and I created this post here and hoping that I will get the right solution for this.

Regards
Ajay

Is it possible you’re using an earlier version of the MongoDB driver? later versions use retryable reads by default https://docs.mongodb.com/manual/core/retryable-reads/

Hi Andrew,
Thanks for your reply.
There was only one single read request sent from my application , so I am not sure any use case of multiple reads here.
May be Mongo atlas have any mechanism for multiple reads for single read request from the replica set or any network issue at mongo end due to that it required retry able reads .

To eliminate any possibilities I checked the driver version using with the application and it is Node.js 3.1.13 driver that is compatible with MongoDB 4.0 .
Should is update my driver, if yes then 3.6.5 will be a good choice ?

The API interfaces are common between 3.1.13 and 3.6.5 or is there any interface change those need to be updated at the application level ?

Regards
Ajay

Hi @Ajay_P,

Wondering whether you have had any success in connecting to your Atlas cluster ?

Should is update my driver, if yes then 3.6.5 will be a good choice ?

It would be ideal if you could update your MongoDB Node.JS driver to the current stable version (v3.6.6). Below is a simple example snippet:

var run = async function() {
  var conn = await MongoClient.connect('mongodb+srv://user:pwd@cluster-1234.mongodb.net/test?retryWrites=true', { useNewUrlParser: true, useUnifiedTopology: true })
  console.log(await conn.db('test').collection('test').countDocuments({}));
}().catch(function(err) {
  console.log(err)
})

If you are still encountering the same issue, could you please provide a minimal reproducible code example.

Regards,
Wan.

1 Like