简体   繁体   中英

How to correct document validation fail in Mongodb?

Ive created the following collection(the creation of which is successful)

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"}}}}}}});

Ive created a set of data to insert into this collection to test the validations


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"}}

     });

However when i run this script i get an error

WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 121,
        "errmsg" : "Document failed validation"
    }
})

Sadly Mongodb isnt very specific in what causes such errors and ive gone through my syntax and declarations and could not pick up on any errors myself,when clearly there is!

Why am i recieving this error when running my code? Thankyou

Try this:

01) Schema:

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"}
                                }
                            }
                        }
                    },
                }
            }
        }
    }
);

02) Insert:

db.Company.insert(
{  
    "city_name": "Sydney",
    "city": "Sydney",
    "street_name": "Pitt Street",
    "building_number": NumberInt(100),
    "budget": 100000.0,
    "Department":{
        "Department_name":"Google",
        "floor_number": NumberInt(4),
        "Employee" : {
            "first_name" : "George",
            "last_name": "Martin",
            "DOB": new Date('Dec 26,1981'),  
            "Salary" : 70000.0,
            "Position": "CEO"
        }
    },    
});

I have to do a few changes:

  • 'int' fields have to be NumberInt(number) in the insert command.
  • The scheme has been changed so that 'Employee' is within 'Department'.
  • Salary must be double.

"Salary": "70000" is an int , but the schema ask for double : "Salary":{"bsonType":"double", "description":"Salary of Employee",minimum: 0}, .

I would recommend that you use the alias "bsonType":"number" in your schema instead of int , double , long , decimal . Since javascript is not typed, it can be a real pain to keep track which is used in your code.

See doc: https://docs.mongodb.com/manual/reference/operator/query/type/#available-types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM