【问题标题】:How to correct document validation fail in Mongodb?如何纠正 Mongodb 中的文档验证失败?
【发布时间】:2019-10-30 11:54:20
【问题描述】:

我创建了以下集合(创建成功)

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

我已经创建了一组数据插入到这个集合中来测试验证


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

     });

但是当我运行这个脚本时,我得到一个错误

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

遗憾的是,Mongodb 对导致此类错误的原因不是很具体,我仔细阅读了我的语法和声明,并且自己无法发现任何错误,但显然有!

为什么在运行我的代码时会收到此错误?谢谢你

【问题讨论】:

    标签: mongodb validation collections


    【解决方案1】:

    试试这个:

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

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

    我必须做一些改变:

    • 'int' 字段在插入命令中必须是 NumberInt(number)。
    • 方案已更改,因此“员工”在“部门”内。
    • 工资必须是双倍的。

    【讨论】:

    • 谢谢!我也可能会问,是使用嵌套的 createCollection Schema 吗?因为我想让部门嵌套在公司内,然后员工嵌套在部门内
    • 是的,在 createCollectionSchema 中您可以创建带有嵌套对象的结构!
    【解决方案2】:

    "Salary" : "70000"int,但架构要求double"Salary":{"bsonType":"double", "description":"Salary of Employee",minimum: 0},

    我建议您在架构中使用别名 "bsonType":"number",而不是 intdoublelongdecimal。由于没有输入 javascript,因此跟踪代码中使用的内容可能会很痛苦。

    见文档:https://docs.mongodb.com/manual/reference/operator/query/type/#available-types

    【讨论】:

    • 啊,好吧。我不认为它会对此敏感。我来自 java 背景,所以声明一个整数是我的第二天性
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多