简体   繁体   English

如何更正 Mongodb 中的文档验证失败?

[英]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!可悲的是,Mongodb 对导致此类错误的原因不是很具体,我仔细阅读了我的语法和声明,并且自己无法发现任何错误,但显然有!

Why am i recieving this error when running my code?为什么我在运行代码时收到此错误? Thankyou谢谢

Try this:尝试这个:

01) Schema: 01)架构:

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: 02)插入:

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. 'int' 字段必须是插入命令中的 NumberInt(number)。
  • 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}, . "Salary": "70000"是一个int ,但模式要求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 .我建议您在架构中使用别名"bsonType":"number"而不是intdoublelongdecimal Since javascript is not typed, it can be a real pain to keep track which is used in your code.由于没有输入 javascript,因此跟踪代码中使用的内容可能会非常痛苦。

See doc: https://docs.mongodb.com/manual/reference/operator/query/type/#available-types请参阅文档: https://docs.mongodb.com/manual/reference/operator/query/type/#available-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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