'Mongodb pool destroyed

Sometimes the mongod service restarts on server and it makes the mongoose to throw an exception:

MongoError: pool destroyed
    at Pool.write (node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:844:8)

Is there a way to automatically detect that the pool has been destroyed and force the server to do a reconnection or a restart?

I mean, in the code there are lots of operations against db and would be a disaster if I should check every operation to db in order to see if connection is still alive, so I'm looking for a solution that could monitor the connection and catch all pool destroyed errors, so I could force the server to restart.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source