关于MongoDB validation的介绍可以看一下我的这篇文章:
https://blog.csdn.net/Jifu_M/article/details/112595879
项目介绍
考虑以下数据库的概念框图,该数据库应该包含有关公司、部门和部门内工作的员工的信息。
该任务的目标是创建一个新的集合JFM,其中包含由上面的概念模式表示的信息。
使用createCollection方法创建一个集合JFM,并使用validator键对集合强制执行以下约束:
(1) 有关部门的信息应嵌套在有关公司的信息中。
(2) 部门员工信息应嵌套在部门信息中。
(3) 与关键字name(在公司类内)、cname、city、street、dname(在部门类内)、first_name、last_name、position(在员工内)关联的值必须是string类型。
(4) 与关键字 budget (在预算内)、salary(在员工内)关联的值必须是double类型。
(5) 与关键字 bldg关联的值必须是整数类型。
(6) 与 关键字floor关联的值必须是整数类型。
(7) 与关键字enumber关联的值必须是整数类型。
(8) 关键字position是可选的,所有其他键都是manadatory(强制的)。
(9) 与关键字bldg相关的值必须为正且小于500。
(10) 与关键字budget和salary相关联的值必须为正。
接下来,向集合JFM中插入两个示例文档。第一个文档必须通过上述约束的所有验证。第二个文档必须在上面列出的其中一个约束的验证中失败。
项目开始
db.createCollection("JFM",
{"validator":{ "$and":[
{"cname":{"$type":"string"}},
{"city":{"$type":"string"}},
{"street":{"$type":"string"}},
{"department.dname":{"$type":"string"}},
{"department.employee.first_name":{"$type":"string"}},
{"department.employee.last_name":{"$type":"string"}},
{"department.employee.position":{"$type":"string"}},
/*关键字 budget (在预算内)、salary(在员工内)关联的值必须是double类型*/
{"budget":{"$type":"double"}},
{"department.employee.salary":{"$type":"double"}},
/*关键字 bldg关联的值必须是整数类型*/
{"bldg":{"$type":"int"}},
/*关键字floor关联的值必须是整数类型*/
{"department.floor":{"$type":"int"}},
/* 关键字enumber关联的值必须是整数类型*/
{"department.employee.enumber":{"$type":"int"}},
/* 关键字position是可选的,所有其他键都是manadatory(强制的)*/
{"$and":[{"department.employee.enumber":{"$exists":true}},
{"department.employee.first_name":{"$exists":true}},
{"department.employee.last_name":{"$exists":true}},
{"department.employee.date_of_birth":{"$exists":true}},
{"$or":[{"department.employee.position": {"$type":"string"}},
{"department.employee.position":{"$exists":false}}]},
{"department.employee.salary":{"$exists":true}} ] },
/* 关键字bldg相关的值必须为正且小于500*/
{"$and":[{"bldg":{"$gt":0}},
{"bldg":{"$lt":500}} ]},
/* 关键字budget和salary相关联的值必须为正*/
{"$and":[{"budget":{"$gt":0}},
{"department.employee.salary":{"$gt":0}} ]}
]
}
} );
集合创建成功后系统会显示:
{ "ok" : 1 }
接下来插入两个文档,第一个文档通过验证,第二个文档被验证拒绝:
> db.JFM.insert(
{
"cname":"Gooogle",
"city":"Chengdu",
"street":"Chenghuabigstreet",
"bldg":NumberInt(400),
"budget":5000.12,
"department":{"dname":"game_development",
"floor":NumberInt(4),
"employee": {"enumber":NumberInt(110),
"first_name": "jf",
"last_name": "M",
"date_of_birth": "2021/01/01",
"position": "engineer",
"salary": 6666.66
}
}
}
);
第一个文档插入成功:
WriteResult({ "nInserted" : 1 })
第二个文档:
> /* The second document must fail validation of one of the constraints listed above.*/;
>
> db.task3.insert(
{ "cname":"Orical",
"city":"Sydney",
"street":"King_rd",
"bldg":NumberInt(666666666),
"budget":4444.12,
"department":{"dname":"software_development",
"floor":NumberInt(3),
"employee": {"enumber":NumberInt(222),
"first_name": "Dong",
"last_name": "Lady",
"date_of_birth": "1990/08/08",
"position": "tester",
"salary": 4444.44}}});
第二个文档因为与关键字bldg相关的值没有小于500,所以被拒绝插入。
报错如下:
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})