Удалить условие соответствия документа в множественной коллекции MongoDB - PullRequest
0 голосов
/ 26 мая 2020

У меня есть архитектура базы данных в MongoDB, как показано ниже:

enter image description here

Я хочу, чтобы при удалении одного документа в коллекции company_info он должен удалить весь документ которые имеют тот же com_id с удаленным документом в коллекции company_info.

Например, когда я удаляю документ с coimid = DHBK в коллекции company_info, mon go оболочка должна удалить весь документ в коллекции отношений (users , company_user_role, ...) который имеет comid = DHBK

Я не знаю оболочки mon go для выполнения этого действия.

Вот пример моей базы данных:

  1. Информация о инкассаторской компании:
{"_id":{"$oid":"5eb8bd2480e33d0f04876616"},"comid":"DHBK","comshortname":"DHQG DHBK","comfullname":"BK University","comaddress":"268 Lý Thường Kiệt HCM","comtel":"028123456789","compersoncontact":"Lê Tiến Thường","compersontel":"028123456789","compersonemail":"thuong@bk.com.vn","comstatus":"ACTIVE","comdate":"2020-05-11"}
{"_id":{"$oid":"5ebe02bcb4146803d8ad255f"},"comid":"DHKT","comshortname":"DHKT","comfullname":"University of Economics Ho Chi Minh City","comaddress":"59C Nguyễn Đình Chiểu, Phường 6, Quận 3, Hồ Chí Minh","comtel":"028123456789","compersoncontact":"Nguyễn Trần Thanh Lâm","compersontel":"028123456789","compersonemail":"thanhlam@bk.com.vn","comstatus":"ACTIVE","comdate":"2020-05-11"}
{"_id":{"$oid":"5ecccb3bb8d5570916cf2916"},"comid":"DHYD","comshortname":"DHYD","comfullname":"Đại học Y Dược TP.HCM","comaddress":"215 Hồng Bàng, Phường 11, Quận 5, Hồ Chí Minh","comtel":"028123456789","compersoncontact":"Donal Trump","compersontel":"028123456789","compersonemail":"trump@america.com.vn","comstatus":"ACTIVE","comdate":"2020-05-26"}
Коллекция пользователей:
{"_id":{"$oid":"5ebd05b52f3700008500220b"},"username":"DHBK","password":"123456","lastname":"DHBK","useremail":"dhbk@edu.com.vn","usertel":"0907111001","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":null,"comid":"DHBK","comdepartment":"DHBK","usercode":"DHBK_0001","usertype":"ADMIN_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220c"},"username":"KHOA_DIEN","password":"123456","lastname":"KHOA_DIEN","useremail":"KHOA_DIEN@edu.com.vn","usertel":"0907111002","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_DIEN","usercode":"DHBK_0002","usertype":"USER_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220d"},"username":"KHOA_XD","password":"123456","lastname":"KHOA_XD","useremail":"KHOA_XD@edu.com.vn","usertel":"0907111003","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_XD","usercode":"DHBK_0003","usertype":"USER_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220e"},"username":"KHOA_CNTT","password":"123456","lastname":"KHOA_CNTT","useremail":"KHOA_CNTT@edu.com.vn","usertel":"0907111004","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_CNTT","usercode":"DHBK_0004","usertype":"USER_COM"}
Компания-сборщик_роль_пользователя
{"_id":{"$oid":"5eb8c6d580e33d0f0487661a"},"rolecode":"DHBK_ROLE_01,DHBK_ROLE_02,DHBK_ROLE_03","comid":"DHBK","username":"DHBK","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe01b5b4146803d8ad255c"},"rolecode":"DHBK_ROLE_01,DHBK_ROLE_03","comid":"DHBK","username":"KHOA_DIEN","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe020ab4146803d8ad255d"},"rolecode":"DHKT_ROLE_01,DHKT_ROLE_02","comid":"DHKT","username":"DHKT","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe025ab4146803d8ad255e"},"rolecode":"DHKT_ROLE_02","comid":"DHKT","username":"GV_BMKTTHUE","datecreate":"2020-05-13"}
Роль компании по сбору:
{"_id":{"$oid":"5eb8c41830cb8651e0906d7c"},"rolecode":"DHBK_ROLE_01","rolename":" ROLE 01","rolelevel":"1","roledesc":"ROLE 01","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c42630cb8651e0906d7d"},"rolecode":"DHBK_ROLE_02","rolename":" ROLE 02","rolelevel":"1","roledesc":"ROLE 02","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdff28b4146803d8ad2556"},"rolecode":"DHBK_ROLE_03","rolename":" ROLE 03","rolelevel":"1","roledesc":"ROLE 03","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdff46b4146803d8ad2557"},"rolecode":"DHKT_ROLE_01","rolename":" ROLE 01","rolelevel":"1","roledesc":"ROLE 01","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHKT"}
{"_id":{"$oid":"5ebdff55b4146803d8ad2558"},"rolecode":"DHKT_ROLE_02","rolename":" ROLE 02","rolelevel":"1","roledesc":"ROLE 02","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHKT"}
Функция коллекторской компании:
{"_id":{"$oid":"5eb8c35b30cb8651e0906d79"},"functioncode":"DHBK_FUNC_01","functionname":"QUYEN 01","functiondesc":"QUYEN 01","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c36b30cb8651e0906d7a"},"functioncode":"DHBK_FUNC_02","functionname":"QUYEN 02","functiondesc":"QUYEN 02","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c37830cb8651e0906d7b"},"functioncode":"DHBK_FUNC_03","functionname":"QUYEN 03","functiondesc":"QUYEN 03","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdfedbb4146803d8ad2554"},"functioncode":"DHKT_FUNC_01","functionname":"QUYEN 01","functiondesc":"QUYEN 01","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHKT"}
{"_id":{"$oid":"5ebdfef6b4146803d8ad2555"},"function_code":"DHKT_FUNC_02","function_name":"QUYEN 02","function_desc":"QUYEN 02","function_status":"ACTIVE","function_date":"2020-05-11","com_id":"DHKT"}
Функция-роля_компании сбора:
{"_id":{"$oid":"5eb8c55230cb8651e0906d7e"},"rolecode":"DHBK_ROLE_01","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02","productid":"mBaaS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5eb8c5a030cb8651e0906d7f"},"rolecode":"DHBK_ROLE_02","functioncode":"DHBK_FUNC_02, DHBK_FUNC_03","productid":"GIS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ebe0016b4146803d8ad2559"},"rolecode":"DHKT_ROLE_01","functioncode":"DHKT_FUNC_01, DHKT_FUNC_02","productid":"mBaaS_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe003cb4146803d8ad255a"},"rolecode":"DHKT_ROLE_02","functioncode":"DHKT_FUNC_01","productid":"Analysis_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe00ffb4146803d8ad255b"},"rolecode":"DHBK_ROLE_03","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK"}
Коллекционная компания_продукт
{"_id":{"$oid":"5eb8c09180e33d0f04876618"},"comid":"DHBK","productid":"GIS_Platform","contractcode":"DHBK_CONTRACT_1","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-06"}
{"_id":{"$oid":"5eb8c0c580e33d0f04876619"},"comid":"DHBK","productid":"mBaaS_Platform","contractcode":"DHBK_CONTRACT_2","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-15"}
{"_id":{"$oid":"5ebdfda8d57326595c694904"},"comid":"DHKT","productid":"mBaaS_Platform,Analysis_Platform","contractcode":"DHKT_CONTRACT_1","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-15"}
{"_id":{"$oid":"5ec78a66b8d5570916cbc0b3"},"comid":"DHBK","productid":"ORIMX_platform","contractcode":"22052020-TEST","contractdate":"2020-05-20","contractstatus":"ACTIVE","datecreate":"2020-05-22"}
{"_id":{"$oid":"5ecc7987b8d5570916ceda21"},"comid":"DHBK1","productid":"ORIMX_platform","contractcode":"11111111","contractdate":"22052020","contractstatus":"DISABLE","datecreate":"22052020"}

1 Ответ

1 голос
/ 26 мая 2020

На данный момент Mon go не поддерживает операцию каскадного удаления. Пн go просто делает то, что ему сказали. Каскадное удаление не является концепцией MongoDB, это скорее концепция SQL.

Итак, на данный момент вы можете вручную удалить соответствующую запись в каждой коллекции. Вы можете использовать следующую команду для удаления документа:

db.Users.remove({coimid: 'DHBK'})
db.Company_user_role.remove({coimid: 'DHBK'})
... // And so on...

Есть другой способ, но это долгий путь. Что вы можете сделать, так это изменить свою схему. Вместо того, чтобы создавать разные коллекции, вы можете собрать все в одну коллекцию. Но я бы порекомендовал вам сделать это, только если ваши коллекции подключены и они часто обновляются.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...