MongoDB 5.0 adds detailed explanations when a document fails schema validation.
Here is an example of validation error with details: https://docs.mongodb.com/manual/core/schema-validation/#existing-documents
"details" : [
{
"operatorName" : "bsonType",
"specifiedAs" : {
"bsonType" : "string"
},
"reason" : "type did not match",
"consideredValue" : 10,
"consideredType" : "double"
}
But I can't get any detailed error messagea on my server.
mongod version: v5.0.2
Here is a simple example on Python:
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
db = client["pymongo"]
schema = {"$jsonSchema": {"required": ["name", "value"]}}
col = db["t0"]
col.drop()
col = db.create_collection("t0", validator=schema)
col.insert_one({"_id": 1, "name": "n1", "value": 2})
# insert ok
col.insert_one({"_id": 2, "name": "n2"})
# this fails with the error
Here is the error, no details:
pymongo.errors.WriteError: Document failed validation, full error: {'index': 0, 'code': 121, 'errmsg': 'Document failed validation'}
I tried insert such invalid document in mongo cli client, in MongoDB Compass. There are errors without details again.
This my database was upgraded from 4.4 version. Maybe it's necessary to turn on some setting on my cluster to show detailed error messages for schema validation?