使用 JSON 結構定義驗證 - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 JSON 結構定義驗證

使用$jsonSchema評估查詢運算子,您可以驗證要插入集合的文件。

建立和使用 JSON 結構描述驗證

使用結構描述驗證建立集合

您可以使用createCollection作業和驗證規則建立集合。這些驗證規則會在插入或更新 Amazon DocumentDB 文件期間套用。下列程式碼範例顯示員工集合的驗證規則:

db.createCollection("employees", { "validator": { "$jsonSchema": { "bsonType": "object", "title": "employee validation", "required": [ "name", "employeeId"], "properties": { "name": { "bsonType": "object", "properties": { "firstName": { "bsonType": ["string"] }, "lastName": { "bsonType": ["string"] } }, "additionalProperties" : false }, "employeeId": { "bsonType": "string", "description": "Unique Identifier for employee" }, "salary": { "bsonType": "double" }, "age": { "bsonType": "number" } }, "additionalProperties" : true } }, "validationLevel": "strict", "validationAction": "error" } )

插入有效的文件

下列範例會插入符合上述結構描述驗證規則的文件:

db.employees.insert({"name" : { "firstName" : "Carol" , "lastName" : "Smith"}, "employeeId": "c720a" , "salary": 1000.0 }) db.employees.insert({ "name" : { "firstName" : "William", "lastName" : "Taylor" }, "employeeId" : "c721a", "age" : 24})

插入無效的文件

下列範例會插入不符合上述結構描述驗證規則的文件。在這個例子中,員工 ID 值不是一個字符串:

db.employees.insert({ "name" : { "firstName" : "Carol" , "lastName" : "Smith"}, "employeeId": 720 , "salary": 1000.0 })

此範例顯示文件中的語法不正確。

修改集合

collMod命令用於添加或修改現有集合的驗證規則。下面的例子添加了一個工資字段的必填字段列表:

db.runCommand({"collMod" : "employees", "validator": { "$jsonSchema": { "bsonType": "object", "title": "employee validation", "required": [ "name", "employeeId", "salary"], "properties": { "name": { "bsonType": "object", "properties": { "firstName": { "bsonType": ["string"] }, "lastName": { "bsonType": ["string"] } }, "additionalProperties" : false }, "employeeId": { "bsonType": "string", "description": "Unique Identifier for employee" }, "salary": { "bsonType": "double" }, "age": { "bsonType": "number" } }, "additionalProperties" : true } } } )

處理驗證規則變更前新增的文件

若要處理在變更驗證規則之前新增至您收集的文件,請使用下列validationLevel修正因子:

  • strict:對所有插入和更新套用驗證規則。

  • 中度:將驗證規則套用至現有有效文件。在更新期間,不會檢查現有的無效文件。

在下列範例中,更新名為「employee」集合的驗證規則之後,薪資欄位是必要的。更新下列文件將會失敗:

db.runCommand({ update: "employees", updates: [{ q: { "employeeId": "c721a" }, u: { age: 25 , salary : 1000}, upsert: true }] })

Amazon DocumentDB 返回以下輸出:

{ "n" : 0, "nModified" : 0, "writeErrors" : [ { "index" : 0, "code" : 121, "errmsg" : "Document failed validation" } ], "ok" : 1, "operationTime" : Timestamp(1234567890, 1) }

moderate將驗證層級更新為允許成功更新上述文件:

db.runCommand({ "collMod" : "employees", validationLevel : "moderate" }) db.runCommand({ update: "employees", updates: [{ q: { "employeeId": "c721a" }, u: { age: 25 , salary : 1000}, upsert: true }] })

Amazon DocumentDB 返回以下輸出:

{ "n" : 1, "nModified" : 1, "ok" : 1, "operationTime" : Timestamp(1234567890, 1) }

使用 $ J 綱要檢索文檔

$jsonSchema運算子可當做篩選器來查詢符合 JSON 結構描述的文件。這是一個頂級運算符,它可以出現在過濾器文檔中作為頂級字段或與查詢運算符(如$and$or,和)一起使用$nor。下列範例會示範使用 $JsonSchema 做為個別篩選器和其他篩選運算子:

插入到一個「員工」集合的文檔:

{ "name" : { "firstName" : "Carol", "lastName" : "Smith" }, "employeeId" : "c720a", "salary" : 1000 } { "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "c720b", "age" : 25, "salary" : 1050.2 } { "name" : { "firstName" : "William", "lastName" : "Taylor" }, "employeeId" : "c721a", "age" : 24, "salary" : 1400.5 } { "name" : { "firstName" : "Jane", "lastName" : "Doe" }, "employeeId" : "c721a", "salary" : 1300 }

僅使用$jsonSchema運算符過濾的集合:

db.employees.find({ $jsonSchema: { required: ["age"] } })

Amazon DocumentDB 返回以下輸出:

{ "_id" : ObjectId("64e5f91c6218c620cf0e8f8b"), "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "c720b", "age" : 25, "salary" : 1050.2 } { "_id" : ObjectId("64e5f94e6218c620cf0e8f8c"), "name" : { "firstName" : "William", "lastName" : "Taylor" }, "employeeId" : "c721a", "age" : 24, "salary" : 1400.5 }

使用$jsonSchema運算子和另一個運算子篩選的集合:

db.employees.find({ $or: [{ $jsonSchema: { required: ["age", "name"]}}, { salary: { $lte:1000}}]});

Amazon DocumentDB 返回以下輸出:

{ "_id" : ObjectId("64e5f8886218c620cf0e8f8a"), "name" : { "firstName" : "Carol", "lastName" : "Smith" }, "employeeId" : "c720a", "salary" : 1000 } { "_id" : ObjectId("64e5f91c6218c620cf0e8f8b"), "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "c720b", "age" : 25, "salary" : 1050.2 } { "_id" : ObjectId("64e5f94e6218c620cf0e8f8c"), "name" : { "firstName" : "William", "lastName" : "Taylor" }, "employeeId" : "c721a", "age" : 24, "salary" : 1400.5 }

使用$jsonSchema運算符和聚合過濾器過濾的集合:$match

db.employees.aggregate( [{ $match: { $jsonSchema: { required: ["name", "employeeId"], properties: {"salary" :{"bsonType": "double"}} } } }] )

Amazon DocumentDB 返回以下輸出:

{ "_id" : ObjectId("64e5f8886218c620cf0e8f8a"), "name" : { "firstName" : "Carol", "lastName" : "Smith" }, "employeeId" : "c720a", "salary" : 1000 } { "_id" : ObjectId("64e5f91c6218c620cf0e8f8b"), "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "c720b", "age" : 25, "salary" : 1050.2 } { "_id" : ObjectId("64e5f94e6218c620cf0e8f8c"), "name" : { "firstName" : "William", "lastName" : "Taylor" }, "employeeId" : "c721a", "age" : 24, "salary" : 1400.5 } { "_id" : ObjectId("64e5f9786218c620cf0e8f8d"), "name" : { "firstName" : "Jane", "lastName" : "Doe" }, "employeeId" : "c721a", "salary" : 1300 }

檢視現有的驗證規則

若要查看集合上現有的驗證規則,請使用:

db.runCommand({ listCollections: 1, filter: { name: 'employees' } })

Amazon DocumentDB 返回以下輸出:

{ "waitedMS" : NumberLong(0), "cursor" : { "firstBatch" : [ { "name" : "employees", "type" : "collection", "options" : { "autoIndexId" : true, "capped" : false, "validator" : { "$jsonSchema" : { "bsonType" : "object", "title" : "employee validation", "required" : [ "name", "employeeId", "salary" ], "properties" : { "name" : { "bsonType" : "object", "properties" : { "firstName" : { "bsonType" : [ "string" ] }, "lastName" : { "bsonType" : [ "string" ] } }, "additionalProperties" : false }, "employeeId" : { "bsonType" : "string", "description" : "Unique Identifier for employee" }, "salary" : { "bsonType" : "double" }, "age" : { "bsonType" : "number" } }, "additionalProperties" : true } }, "validationLevel" : "moderate", "validationAction" : "error" }, "info" : { "readOnly" : false }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.employees" } } ], "id" : NumberLong(0), "ns" : "test.$cmd.listCollections" }, "ok" : 1, "operationTime" : Timestamp(1692788937, 1) }

Amazon DocumentDB 也會在$out彙總階段保留驗證規則。

支援關鍵字

createcollMod指令支援下列欄位:

  • Validator— 支援 $jsonSchem a 運算子。

  • ValidationLevel— 支援offstrict、和moderate值。

  • ValidationAction— 支援error值。

該 $jsonSchema 運算符支持以下關鍵字:

  • additionalItems

  • additionalProperties

  • allOf

  • anyOf

  • bsonType

  • dependencies

  • description

  • enum

  • exclusiveMaximum

  • exclusiveMinimum

  • items

  • maximum

  • minimum

  • maxItems

  • minItems

  • maxLength

  • minLength

  • maxProperties

  • minProperties

  • multipleOf

  • not

  • oneOf

  • pattern

  • patternProperties

  • properties

  • required

  • title

  • type

  • uniqueItems

bypassDocumentValidation

Amazon DocumentDB 支持bypassDocumentValidation以下命令和方法:

  • insert

  • update

  • findAndModify

  • $outaggregate指令和db.collection.aggregate()方法中的階段

Amazon DocumentDB 不支持以下命令:bypassDocumentValidation

  • $mergeaggregate命令和db.collection.aggregate()方法中

  • mapReduce命令和db.collection.mapReduce()方法

  • applyOps 命令

限制

下列限制適用於$jsonSchema驗證:

  • 當操作失敗驗證規則時,Amazon DocumentDB 會傳回錯誤「文件驗證失敗」。

  • 不支援 Amazon DocumentDB 彈性叢集。$jsonSchema