Mongo 3.6 使用jsonSchema调试验证
Mongo 3.6 debug validation using jsonSchema
我正在学习 mongodb 大学的课程以学习 3.6 版中的新功能,但我无法解决为什么我的验证器无效。
这就是我创建 collection 的方式:
db.getSiblingDB("TSA").createCollection("claims", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
_id: { },
airportCode: { type: "string", minLength: 3 },
airportName: { type: "string" },
airlineName: { type: "string", minLength: 5 },
claims: {
bsonType: "object",
properties: {
itemCategory: { bsonType: "array", maxItems: 3 },
amount: { type: "string", pattern: "^$.*" }
}
}
},
required: ["airportCode", "airlineName", "claims"],
additionalProperties: false
}
}
})
然后,我尝试插入这个 object:
db.getSiblingDB("TSA").claims.insertOne({
"airportCode": "ABE",
"airportName": "Lehigh Valley International Airport, Allentown",
"airlineName": "MongoAir",
"claims": {
"claimType": "Property Damage",
"claimSite": "Checked Baggage",
"itemCategory": [ "Sporting Equipment & Supplies" ],
"amount": "0.00"
}
})
出现以下错误:
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5a705318d3d6c18337f07282"),
"airportCode" : "ABE",
"airportName" : "Lehigh Valley International Airport, Allentown",
"airlineName" : "MongoAir",
"claims" : {
"claimType" : "Property Damage",
"claimSite" : "Checked Baggage",
"itemCategory" : [
"Sporting Equipment & Supplies"
],
"amount" : "0.00"
}
}
})
我的问题是,是否有一些方法可以像 "property X must be Y type" 一样调试验证器而不是获取通用的 "Document failed validation"?
从 MongoDB 3.6 开始,没有反馈机制可以通知在服务器端检查期间文档的哪一部分验证失败。相应的功能请求已打开:SERVER-20547: Expose the reason an operation fails document validation。目前,如果需要详细反馈,则由应用程序代码自行验证。
如果您使用正则表达式模式字符串,反斜杠本身也必须转义:
db.getSiblingDB("TSA").createCollection("claims", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
_id: { },
airportCode: { type: "string", minLength: 3 },
airportName: { type: "string" },
airlineName: { type: "string", minLength: 5 },
claims: {
bsonType: "object",
properties: {
itemCategory: { bsonType: "array", maxItems: 3 },
amount: { type: "string", pattern: "^\$.*" }
}
}
},
required: ["airportCode", "airlineName", "claims"],
additionalProperties: false
}
}
})
我正在学习 mongodb 大学的课程以学习 3.6 版中的新功能,但我无法解决为什么我的验证器无效。
这就是我创建 collection 的方式:
db.getSiblingDB("TSA").createCollection("claims", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
_id: { },
airportCode: { type: "string", minLength: 3 },
airportName: { type: "string" },
airlineName: { type: "string", minLength: 5 },
claims: {
bsonType: "object",
properties: {
itemCategory: { bsonType: "array", maxItems: 3 },
amount: { type: "string", pattern: "^$.*" }
}
}
},
required: ["airportCode", "airlineName", "claims"],
additionalProperties: false
}
}
})
然后,我尝试插入这个 object:
db.getSiblingDB("TSA").claims.insertOne({
"airportCode": "ABE",
"airportName": "Lehigh Valley International Airport, Allentown",
"airlineName": "MongoAir",
"claims": {
"claimType": "Property Damage",
"claimSite": "Checked Baggage",
"itemCategory": [ "Sporting Equipment & Supplies" ],
"amount": "0.00"
}
})
出现以下错误:
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5a705318d3d6c18337f07282"),
"airportCode" : "ABE",
"airportName" : "Lehigh Valley International Airport, Allentown",
"airlineName" : "MongoAir",
"claims" : {
"claimType" : "Property Damage",
"claimSite" : "Checked Baggage",
"itemCategory" : [
"Sporting Equipment & Supplies"
],
"amount" : "0.00"
}
}
})
我的问题是,是否有一些方法可以像 "property X must be Y type" 一样调试验证器而不是获取通用的 "Document failed validation"?
从 MongoDB 3.6 开始,没有反馈机制可以通知在服务器端检查期间文档的哪一部分验证失败。相应的功能请求已打开:SERVER-20547: Expose the reason an operation fails document validation。目前,如果需要详细反馈,则由应用程序代码自行验证。
如果您使用正则表达式模式字符串,反斜杠本身也必须转义:
db.getSiblingDB("TSA").createCollection("claims", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
_id: { },
airportCode: { type: "string", minLength: 3 },
airportName: { type: "string" },
airlineName: { type: "string", minLength: 5 },
claims: {
bsonType: "object",
properties: {
itemCategory: { bsonType: "array", maxItems: 3 },
amount: { type: "string", pattern: "^\$.*" }
}
}
},
required: ["airportCode", "airlineName", "claims"],
additionalProperties: false
}
}
})