Q54 [Scenario]: A user registration endpoint throws MongoServerError E11000 duplicate key error. How should your application handle this?

Catch the E11000 error code inside Express error-handling middleware. Parse the error.keyPattern or error.keyValue object to identify which field caused the collision (e.g., email), and return a user-friendly HTTP 409 Conflict status response ({ message: 'Email address already registered' }).

Key Takeaways
  • E11000 is the MongoDB duplicate key error code.
  • Parse err.keyValue to return targeted validation messages.
Knowledge Check

What standard MongoDB error code signifies a unique index duplicate key constraint violation?