MongoDB error topology was destroyed - Fix it now








Basically, the 'MongoDB error topology was destroyed' error occurs if the node server's connection to the MongoDB instance was interrupted while it was trying to write to it.
Or if the mongo driver drops the connection for any reason.
Another reason could be mongoose disconnecting before mongo document indexes are created.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to resolve MongoDB errors.
In this context, we shall look into how to fix this MongoDB error message.

How to fix the error 'MongoDB error topology was destroyed' ?

Recently, one of our customers approached us with a MongoDB error message.

He told that suddenly the node started crapping out errors with the message "MongoDB error: Topology was destroyed".

Now let's take a look at how our Support Experts resolve this error message.
Here, we found that the mongo driver was dropping the connection. So, we can increase the retry time value.
By default, the mongoose will try to reconnect for 30 seconds and then stop retrying.

After that, it throws errors forever until we restart it.
We change this by editing the below fields:

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

This instantly fixed the error for the customer.

Another reason could be mongoose disconnecting before the creation of mongo document indexes.
In order to make sure all models have their indexes built before disconnecting, we can make the below changes:

await Promise.all(mongoose.modelNames().map(model => mongoose.model(model).ensureIndexes()));
await mongoose.disconnect();

This must get rid of the error.

[Need urgent assistance in fixing MongoDB errors? – We are here to help you. ]



Conclusion

This article covers method to fix the error 'MongoDB error topology was destroyed' which happens due to many reasons that include interruption of the node server’s connection to the MongoDB instance while it was trying to write to it. If the mongo driver drops the connection for any reason.
Another reason could be mongoose disconnecting before the creation of mongo document indexes.
You might try updating mongo.
There's also a mongo config setting that can easily in the database using too much memory and being killed by the kernel.


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com