Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Khaled Pendleton
Khaled Pendleton
20,430 Points

How to handle a MongoDB crash?

I'm building an ExpressJS API with Mongo/Mongoose. If my app successfully connects to Mongo but at a later time loses its connection for whatever reason, how do I: 1) Figure this out as soon as it fails 2) Re-establish the connection 3) Figure out how it failed in the first place

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Khaled;

This is not a unique thing to MongoDB as it could happen with any external functionality, service, or API, right? SQL Server connections, web API, etc. could all become unresponsive or unreachable.

So to address your questions:

  1. In your application you will want to, typically, make connections and CRUD operations to your data store (regardless of technology) inside some form of try... catch statement.
  2. Reestablishing a connection might very well be outside your control and would likely need to vary depending on your particular application.
  3. Again, figuring out the why might be something well beyond your control. What if the database server crashed? What if a user's cat unplugged their network connection? MongoDB (and most databases) will have log files available for examination but that is likely beyond the scope of of the application developer specifically.

Post back if you have further questions.