MongoDB插入数据触发文档验证失败错误的原因排查咨询
Problem Description
I successfully created a Company collection with JSON Schema validation using this command:
db.createCollection("Company", { "validator": { "$jsonSchema": { "bsonType":"object", "required":["city_name","city","street_name","building_number","budget","Department"], "properties":{ "city_name":{ "bsonType":"string", "description":"name of city" }, "city":{ "bsonType":"string", "description":"City" }, "street_name":{ "bsonType":"string", "description" :"name of street" }, "building_number":{"bsonType":"int", "description":"number of building", minimum: 0, maximum: 500}, "budget":{"bsonType":"double", "description":"budget of company",minimum: 0 }, "Department":{ "bsonType":"object", "required":["Department_name","floor_number","Employee"], "properties":{ "Department_name":{"bsonType":"string", "description": "name of department" }, "floor_number":{"bsonType":"int", "description":"number of floor" }, }}, "Employee":{ "bsonType":"object", "required":["first_name","last_name","DOB","Salary"], "properties":{"first_name":{"bsonType":"string", "description":"Employees first name"}, "last_name":{"bsonType":"string", "description":"Employees last name"}, "DOB":{"bsonType":"date", "description":"Date of birth of empployee"}, "Salary":{"bsonType":"double", "description":"Salary of Employee",minimum: 0}, "Position":{"bsonType":"string", "description":"Position of employee. Field is not required"}}}}}});
When trying to insert test data with this command:
db.Company.insert( { "city_name":"Sydney", "city":"Sydney", "street_name":"Pitt Street", "building_number":100, "budget": 100000.0, "Department":{"department_name":"Google", "floor_number":4, "Employee" :{"first_name" : "George", "last_name": "Martin", "DOB": new Date('Dec 26,1981'), "Salary" : "70000", "Position": "CEO"}} });
I get the error:
WriteResult({ "nInserted" : 0, "writeError" : { "code" : 121, "errmsg" : "Document failed validation" } })
I can't spot the syntax issue on my own—why is this validation failing?
Solution
There are several clear mismatches between your JSON Schema and the inserted document that are triggering the validation error. Let's break them down:
1. Case-Sensitive Field Name Mismatch
Your schema defines the required field in Department as Department_name (capitalized "D"), but your inserted document uses department_name (lowercase "d"). MongoDB field names are case-sensitive, so the schema sees this required field as missing.
2. Incorrect Data Type for Salary
In your schema, Employee.Salary is specified as a double numeric type, but you're inserting it as a string ("70000"). You need to pass a raw numeric value instead of a quoted string.
3. Misaligned Document Structure
Your schema defines Employee as a top-level required field of the root document, but your inserted document nests Employee inside the Department object. On top of that, your Department schema lists Employee in its required array but doesn't actually define it in the properties object—this is a structural inconsistency.
Corrected Implementation
If you want to keep Employee nested under Department, first update your schema to properly define the nested structure:
db.createCollection("Company", { "validator": { "$jsonSchema": { "bsonType": "object", "required": ["city_name", "city", "street_name", "building_number", "budget", "Department"], "properties": { "city_name": { "bsonType": "string", "description": "name of city" }, "city": { "bsonType": "string", "description": "City" }, "street_name": { "bsonType": "string", "description": "name of street" }, "building_number": { "bsonType": "int", "description": "number of building", "minimum": 0, "maximum": 500 }, "budget": { "bsonType": "double", "description": "budget of company", "minimum": 0 }, "Department": { "bsonType": "object", "required": ["Department_name", "floor_number", "Employee"], "properties": { "Department_name": { "bsonType": "string", "description": "name of department" }, "floor_number": { "bsonType": "int", "description": "number of floor" }, // Add nested Employee definition here "Employee": { "bsonType": "object", "required": ["first_name", "last_name", "DOB", "Salary"], "properties": { "first_name": { "bsonType": "string", "description": "Employees first name" }, "last_name": { "bsonType": "string", "description": "Employees last name" }, "DOB": { "bsonType": "date", "description": "Date of birth of employee" }, "Salary": { "bsonType": "double", "description": "Salary of Employee", "minimum": 0 }, "Position": { "bsonType": "string", "description": "Position of employee. Field is not required" } } } } } // Remove top-level Employee definition since it's now nested } } } });
Then use this corrected insert command:
db.Company.insert({ "city_name": "Sydney", "city": "Sydney", "street_name": "Pitt Street", "building_number": 100, "budget": 100000.0, "Department": { "Department_name": "Google", // Fixed case "floor_number": 4, "Employee": { "first_name": "George", "last_name": "Martin", "DOB": new Date('Dec 26,1981'), "Salary": 70000, // Changed to numeric value "Position": "CEO" } } });
Pro Tip for Debugging
To get more specific validation error messages in the future, use db.runCommand() instead of the basic insert method—it will tell you exactly which rule was violated:
db.runCommand({ insert: "Company", documents: [/* Your test document here */], writeConcern: { w: 1 } });
内容的提问来源于stack exchange,提问作者David Ling

