MongoError: The topology has been destroyed

I have a REST service built into node.js with Restify and Mongoose and mongoDB with a collection containing about 30,000 regular sized documents. I have a node service running via pmx and pm2.

Yesterday, unexpectedly, node began to trim errors with the message "MongoError: the topology was destroyed", nothing more. I have no idea what this means and what could cause it. there is also not much to be found when google search it. So I thought I would ask here.

After restarting the node service, errors stopped coming. I also have one of them working in production, and it scares me that this can happen at any given time in a rather important part of the installation working there ...

I use the following versions of the packages mentioned:

  • mongoose: 4.0.3
  • restify: 3.0.3
  • node: 0.10.25
+144
mongodb mongoose restify pm2
Jun 18 '15 at 7:50
source share
16 answers

It seems your node server connection with your MongoDB instance was interrupted when it tried to write to it.

Take a look at the Mongo source code that generates this error

Mongos.prototype.insert = function(ns, ops, options, callback) { if(typeof options == 'function') callback = options, options = {}; if(this.s.state == DESTROYED) return callback(new MongoError(f('topology was destroyed'))); // Topology is not connected, save the call in the provided store to be // Executed at some point when the handler deems it reconnected if(!this.isConnected() && this.s.disconnectHandler != null) { callback = bindToCurrentDomain(callback); return this.s.disconnectHandler.add('insert', ns, ops, options, callback); } executeWriteOperation(this.s, 'insert', ns, ops, options, callback); } 

This is not like the Sails issue mentioned in the comments, because the updates were not installed to expedite the crash or fix

+92
Aug 11 '15 at 19:12
source share

I know that Jason's answer was accepted, but I had the same problem with Mongoose and found that the service hosting my database is recommended to use the following settings in order to maintain Mongodb communication in production:

 var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } }; mongoose.connect(secrets.db, options); 

I hope this answer can help other people with Topology errors.

+80
Oct 16 '15 at 5:50
source share

This error is caused by the mongo driver disconnecting for some reason (for example, the server was unavailable).

By default, mongoose will try to reconnect for 30 seconds, then stop trying again and throw errors forever before restarting.

You can change this by editing these 2 fields in the connection settings

 mongoose.connect(uri, { server: { // sets how many times to try reconnecting reconnectTries: Number.MAX_VALUE, // sets the delay between every retry (milliseconds) reconnectInterval: 1000 } } ); 

connectivity documentation

+66
Oct 03 '16 at 12:35
source share

In my case, this error was caused by db.close(); from the "expect" section inside the "asynchronous"

 MongoClient.connect(url, {poolSize: 10, reconnectTries: Number.MAX_VALUE, reconnectInterval: 1000}, function(err, db) { // Validate the connection to Mongo assert.equal(null, err); // Query the SQL table querySQL() .then(function (result) { console.log('Print results SQL'); console.log(result); if(result.length > 0){ processArray(db, result) .then(function (result) { console.log('Res'); console.log(result); }) .catch(function (err) { console.log('Err'); console.log(err); }) } else { console.log('Nothing to show in MySQL'); } }) .catch(function (err) { console.log(err); }); db.close(); // <--------------------------------THIS LINE }); 
+16
Aug 25 '18 at 18:25
source share

Just a minor addition to Gaafar's answer, it gave me a warning about obsolescence. Instead of an object on the server, like this:

 MongoClient.connect(MONGO_URL, { server: { reconnectTries: Number.MAX_VALUE, reconnectInterval: 1000 } }); 

This can go to a top level object. Essentially, just extract it from the server object and place it in the parameter object as follows:

 MongoClient.connect(MONGO_URL, { reconnectTries: Number.MAX_VALUE, reconnectInterval: 1000 }); 
+12
Jan 27 '18 at 23:26
source share

"Topology destroyed" may be triggered by disabling mongoose before creating mongo document indexes, according to this comment

To make sure that all models have their own indices built before disconnecting, you can:

 await Promise.all(mongoose.modelNames().map(model => mongoose.model(model).ensureIndexes())); await mongoose.disconnect(); 
+6
Jul 21 '18 at 10:46
source share

I had the same error. Finally, I found that I have some error in my code. I use load balancing for two nodejs servers, but I just update the code of one server.

I am changing the mongod server from standalone to replication , but I forgot to do the corresponding update for the connection string, so I met this error.

separate connection string: mongodb://server-1:27017/mydb replication connection string: mongodb://server-1:27017,server-2:27017,server-3:27017/mydb?replicaSet=myReplSet

more details here: [mongo doc for connection string]

+2
May 11 '16 at 1:40
source share

I met this in kubernetes / minikube + nodejs + mongoose environment. The problem was that the DNS service was running with some delay. DNS check is ready, solved my problem.

 const dns = require('dns'); var dnsTimer = setInterval(() => { dns.lookup('mongo-0.mongo', (err, address, family) => { if (err) { console.log('DNS LOOKUP ERR', err.code ? err.code : err); } else { console.log('DNS LOOKUP: %j family: IPv%s', address, family); clearTimeout(dnsTimer); mongoose.connect(mongoURL, db_options); } }); }, 3000); var db = mongoose.connection; var db_options = { autoReconnect:true, poolSize: 20, socketTimeoutMS: 480000, keepAlive: 300000, keepAliveInitialDelay : 300000, connectTimeoutMS: 30000, reconnectTries: Number.MAX_VALUE, reconnectInterval: 1000, useNewUrlParser: true }; 

(numbers in db_options are arbitrarily found on the stack thread and similar sites)

+2
Aug 13 '18 at 10:14
source share

I got this error when creating a new database in the MongoDb Compass community. The problem was with my Mongod, it did not work. As a fix, I had to execute the Mongod command as described above.

 C:\Program Files\MongoDB\Server\3.6\bin>mongod 

I was able to create the database after running this command.

Hope it helps.

+1
Mar 11 '18 at 14:37
source share

Sebastian’s comment on Adrien’s answer needs more attention, he helped me, but this comment can sometimes be ignored, so here is the solution :

 var options = { useMongoClient: true, keepAlive: 1, connectTimeoutMS: 30000, reconnectTries: 30, reconnectInterval: 5000 } mongoose.connect(config.mongoConnectionString, options, (err) => { if(err) { console.error("Error while connecting", err); } }); 
+1
Mar 31 '19 at 6:30
source share

I struggled with this for some time - as you can see from the other answers, the problem may be completely different.

The easiest way to find out what is causing this is to enable loggingLevel: info in the options

+1
Jun 15 '19 at 7:14
source share

Here is what I did, everything works fine. The problem disappeared after adding the following parameters.

 const dbUrl = "mongodb://localhost:27017/sampledb"; const options = { useMongoClient: true, keepAlive: 1, connectTimeoutMS: 30000, reconnectTries: 30, reconnectInterval: 5000, useNewUrlParser: true } mongoose.connect(dbUrl,options, function( error ) { if (error) { console.log("mongoerror", error); } else { console.log("connected"); } }); 
+1
Aug 22 '19 at 15:46
source share

In my case, this error was caused by the same server instance that was already running in the background.

The strange thing is that when I started my server without prior notice, on which the server was already running, the console did not show anything like "something is using port xxx." I could even upload something to the server. So, it took me quite a while to find this problem.

Moreover, after closing all the applications that I can imagine, I still could not find the process using this port in my Mac activity monitor. I have to use lsof for tracking. The culprit was not surprised - this is a node process. However, using the PID shown in the terminal, I found that the port number on the monitor is different from the port number used by my server.

In general, the destruction of all node processes can solve this problem directly.

0
Sep 01 '18 at 12:02
source share

You need to restart mongo to fix the topology error and then just change some mongoose or mongoclient options to overcome this problem:

 var mongoOptions = { useMongoClient: true, keepAlive: 1, connectTimeoutMS: 30000, reconnectTries: Number.MAX_VALUE, reconnectInterval: 5000, useNewUrlParser: true } mongoose.connect(mongoDevString,mongoOptions); 
0
Aug 26 '19 at 7:03
source share

I solved this problem:

  1. ensuring mongo works
  2. restarting my server
-one
Apr 12 '18 at 23:13
source share

You need to go to the " Connect " menu and click on the " Connect to ... " submenu. A new MongoDB Compass community window opens, through which you can connect again.

-5
Apr 01
source share



All Articles