1. 程式人生 >實用技巧 >其它 >MongoDB專案練習之基於概念框圖的validation設計

MongoDB專案練習之基於概念框圖的validation設計


技術標籤:資料庫系統mongodbvalidation驗證器概念框圖資料庫

MongoDB validation


關於MongoDB validation的介紹可以看一下我的這篇文章:
https://blog.csdn.net/Jifu_M/article/details/112595879

專案介紹

考慮以下資料庫的概念框圖,該資料庫應該包含有關公司、部門和部門內工作的員工的資訊。

MongoDB專案練習之基於概念框圖的validation設計


該任務的目標是建立一個新的集合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"
        }
})