使用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
否定所有NumberInt
和NumberDecimal
标签,而没有任何改变。此外,设置validationAction: "warn"
允许插入文档,但不是所需的功能。同样,从required
对象中删除所有项目。
此架构设计当前存在什么问题?
答案 0 :(得分:1)
尝试以下命令:
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
应该如上所述插入。