20

How do I determine why a MongoDB document insert is failing validation? All I get back is a writeError that says "Document failed validation", which isn't very helpful.

(This happens often, and I'd like to understand how to properly debug these, rather than ask for help with a specific example.)

2 Answers 2

15

As at MongoDB 3.2, there is no feedback on the reason document validation failed: the overall validation expression currently evaluates as either True ("OK") or False ("Document failed validation"). Validation behaviour can be adjusted with validationAction (error/warn) and validationLevel (strict/moderate/off) configuration options, but this does not provide any further context for validation failures.

If you want to have more detailed feedback, the recommended approach would be to add validation logic into your application rather than relying solely on server-side checks. Even with server-side validation, many checks are best done in application business logic to minimize round trips to the database server and provide more responsive feedback to the end user.

For example, user input for a web app (required fields, field formats, ...) should be validated in the browser before being submitted to your application or attempting to insert/update in the database.

However, it does make sense to validate at multiple levels to ensure data quality and some context to diagnose validation failures would be very useful. There is a relevant open feature request you can watch/up-vote in the MongoDB issue tracker: SERVER-20547: Expose the reason an operation fails document validation.

For more information you may also be interested in Document Validation - Part 1: Adding Just the Right Amount of Control Over Your Documents. This highlights some of the general pros & cons of document validation as at MongoDB 3.2, and includes a reference table for the outcome based on validationAction and validationLevel configuration options.

3
  • 2
    Thanks! Guess I wasn't missing anything here. We indeed do most of our validation in our application, but we leave low-level checks as MongoDB validators, mostly to make sure our software engineers (and myself) aren't creating bugs during development, rather than to find user errors in production. I've up-voted the JIRA. Cheers! Commented Aug 23, 2016 at 2:21
  • The problem is the schema format is "semi-standard" i.e. with MongoDB extensions, so it's still hard to find out what is the cause using standard JSON Schema validators. Commented May 24, 2018 at 8:55
  • 1
    @HendyIrawan Yes, there are a few extensions to JSON Schema for MongoDB data types so additional client validation or transformation would be required for strict validation. I've seen a few examples like mongo-schemer for Node.js, which builds on AJV (Another JSON Schema Validator).
    – Stennie
    Commented May 25, 2018 at 3:27
2

Of course the original answer is right, handling validation prior to arriving at the db is absolutely best practice, but as a practical matter if you need to chase it down like right now, you can temporarily remove validation from the Schema and then seeing what shows up in the Collection.

If a field was required but it shows up missing, empty, or misshapen at least it narrows your search. If the data looks right, have a look at the validations specified in the Schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.