MongoDB模式验证失败

良野

我正在尝试使用MongoDB v3.6.2设计一个模式验证(最终将拥有比下面详述的更多的值)。我当前的布局具有以下结构:

架构图

db.createCollection("Products", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: ["ProductId",
                       "ProductName",
                       "Bonus.PremiumRate",
                       "Bonus.InterestRate",
                       "SurrChargeRates.First",
                       "SurrChargeRates.Second",
                       "SurrChargeRates.Third"],
            properties: {
                "ProductId": {
                    bsonType: "int",
                    description: "Must be a numerical representation of ID"
                },
                "ProductName": {
                    bsonType: "string",
                    description: "Must be a string representation of Product Name"
                },
                "Bonus.PremiumRate": {
                    bsonType: "decimal",
                    description: "Must be a decimal representation of Premium Rate Bonus"
                },
                "Bonus.InterestRate": {
                    bsonType: "decimal",
                    description: "Must be a decimal representation of Interest Rate Bonus"
                },
                "SurrChargeRates.First": {
                    bsonType: "decimal",
                    description: "Must be a decimal representation of First Surrender Charge Rate"
                },
                "SurrChargeRates.Second": {
                    bsonType: "decimal",
                    description: "Must be a decimal representation of Second Surrender Charge Rate"
                },
                "SurrChargeRates.Third": {
                    bsonType: "decimal",
                    description: "Must be a decimal representation of Third Surrender Charge Rate"
                }
            }
        }
    },
    validationLevel: "strict",
    validationAction: "error"
});

这被MongoDB接受,并成功创建了收集和验证。然而,当我试图插入我会见了错误代码121的文件,Document failed validation

insert我想目前是以下情况:

插入

db.Products.insert({
    "ProductId": NumberInt(1), 
    "ProductName": "Product Name",
    "Bonus.PremiumRate": NumberDecimal("0.3"),
    "Bonus.InterestRate": NumberDecimal("0.5"),
    "SurrChargeRates.First": NumberDecimal("0.1"),
    "SurrChargeRates.Second": NumberDecimal("0.1"),
    "SurrChargeRates.Third": NumberDecimal("0.1")
});

我也尝试过insert否定所有NumberIntNumberDecimal标签,没有任何变化。此外,设置validationAction: "warn"允许插入文档,但不是所需的功能。同样,从required对象中删除所有项目

此架构设计当前存在什么问题?

尝试以下命令:

db.Products.insert({
    "ProductId": NumberInt(1), 
    "ProductName": "Product Name",
    "Bonus" : {
        "PremiumRate": NumberDecimal("0.3"),
        "InterestRate": NumberDecimal("0.5")
    },
    "SurrChargeRates":{
        "First": NumberDecimal("0.1"),
        "Second": NumberDecimal("0.1"),
        "Third": NumberDecimal("0.1")
    }
});

失败的原因是点符号。Subdocuments应该如上插入。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章