数据库系统之MongoDB Validations

概述

JSON/BSON数据模型的半结构化和无模式属性允许对数据库结构进行非常灵活的操作。

与此同时,数据库结构的灵活和不受控制的操作也很容易造成数据库结构和数据库内容的破坏,例如,由于随机错误。

MongoDB提供了在更新和插入(不是删除)期间验证文档(validate documents)的能力。

验证规则具体来说就是使用createCollection() 方法的validator选项。

可以使用collMod命令在验证器选项中来打开或关闭验证规则。

从3.6版开始,MongoDB支持JSON模式验证。

JSON模式验证是执行模式验证的推荐方法。

类型约束

类型约束确定与键关联的值的类型。
举个例子:
在创建一个集合department时,我们确保与name关联的值是string类型,与budget关联的值是double类型。

db.createCollection("department",
					{"validator":{"name":{"$type":"string"},
					"code":{"$type":"string"},
					"total staff number":{"$type":"int"},
					"budget":{"$type":"double"} }} );

数字的默认类型是double,例如" budget ":2000
下面插入一个文档可以通过验证:

db.department.insert( {"name":"Department of Dust",
						"code":"DOD",
						"total_staff_number":5,
						"budget":2000} );

嵌套结构的类型验证:

db.createCollection("department",
					{"validator":{"address.bldg":{"$type":"double"},
					"address.street":{"$type":"string"},
					"address.city":{"$type":"string"},
					"address.country":{"$type":"string"} }} );

创建针对department里的address验证。

存在约束

存在约束确定特定的键:值对是强制性的还是可选的。
如果是强制的,那么在Insert时必须要包含这个键的值。

关于国家的信息在地址中是可选的:

db.createCollection("department",
					{"validator":{"$or":[{"address.country":{"$type":"string"}},
										{"address.country":{"$exists":false}}
										] }
} );

要么是个string类型,要么可以没有。

一个文档必须包含其中一个电子邮件或地址,而不是两者都包含:

db.createCollection("department",
					{"validator":{"$or":[{"$and":[{"email":{"$exists":true}},
												  {"phone":{"$exists":false}}]},
												  {"$and":[{"email":{"$exists":false}},
												  {"phone":{"$exists":true}}]}
												] }
										} );

两个必须要有一个,但是不能都有。

域约束

域约束可以精确地确定键:值对中的值的取值范围。

预算的值必须是小于或等于1000000的正数:

db.createCollection("department",
					{"validator":{"$and":[{"budget":{"$type":"double"}},
										  {"budget":{"$gt":0}},
										  {"budget":{"$lte":1000000}}
										]}
					} );

JSON模式验证

JSON模式是一个JSON文档,用于定义验证、文档和交互控制的JSON数据结构。

JSON文档的JSON模式验证是通过验证文档的结构和内容与JSON模式的一致性来执行的。

在MongoDB中,操作符$jsonSchema根据给定的JSON模式验证文档。

j s o n S c h e m a 操 作 符 也 可 以 用 于 f i n d 命 令 或 jsonSchema操作符也可以用于find命令或 jsonSchemafindmatch聚合阶段的查询。

举个JSON模式验证的例子:

db.createCollection("department",
					{"validator":
						{$jsonSchema:{"bsonType":"object",
									 "required":["name","address"],
									 "properties":{"name":{"bsonType": "string",
														 "description":"Name of a department" },
												 "address":{"bsonType":"object",
														 "description":"Address of a department",
												 "required":["city","street"],
												 "properties": {"city":{"bsonType":"string",
																	   "description":"City name"},
															"street":{"bsonType":"string",
																	  "description":"Street name"}
					} } } } } });

注意JSON模式验证与其他验证格式的区别。同时,要注意文档的套嵌结构。
在这里中,文档的结构如下:

{ "name":"SCIT",
  "address":{"city":"Chengdu",
					"street":"Er xian qiao."} }

如果要对一个数组进行验证:

months":{"bsonType":"array",
		"description":"array",
		"items":{"bsonType":"string"}

在这里数组里的内容为文字。

validations管理

可以设置不同的验证级别:warn和strict。

验证级别warn接受在日志中记录警告的文档。

db.createCollection("department",
					{"validator":{"name":{"$type":"string"},
								"code":{"$type":"string"},
								"total_staff_number":{"$type":"double"},
								"budget":{"$type":"double"}
								},
                    "validationAction":"warn"} );

上面的验证通过后可以在日志文件的记录中查看警告的文档。

可以向已经存在的集合中添加validator:

db.runCommand({"collMod":"department",
				"validator":{"validator":{"name":{"$type":"string"},
										  "code":{"$type":"string"},
										  "total staff number":{"$type":"double"},
										  "budget":{"$type":"double"}
										  }
							},
				"validationAction":"error",
				"validationLevel": "strict"} );

可以参考一下我的MongoDB Validations项目练习:
https://blog.csdn.net/Jifu_M/article/details/112626948

References

  1. MongoDB Manual, Document validations https://docs.mongodb.com/v3.6/core/schema-validation/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值