ThanhLam112358
ThanhLam112358

Reputation: 917

Delete document matching condition in multi colection MongoDB

I have database architecture in MongoDB like below:

enter image description here

I want when delete one document in colection company_info, it must delete all document which have same com_id with deleted document in colection company_info.

For example, When I delete document with coimid = DHBK in colection company_info, mongo shell must delete all document in relationship collection (users,company_user_role,...) which has comid = DHBK

I don't know mongo shell to do this action.

Here is my database example:

  1. Collection company_info:
{"_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":"[email protected]","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":"[email protected]","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":"[email protected]","comstatus":"ACTIVE","comdate":"2020-05-26"}
  1. Users collection:
{"_id":{"$oid":"5ebd05b52f3700008500220b"},"username":"DHBK","password":"123456","lastname":"DHBK","useremail":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","usertel":"0907111004","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_CNTT","usercode":"DHBK_0004","usertype":"USER_COM"}
  1. Collection Company_user_role
{"_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"}
  1. Collection company_role:
{"_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"}
  1. Collection Company_function:
{"_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"}
  1. Collection Company_role_function:
{"_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"}
  1. Collection Company_product
{"_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"}

Upvotes: 0

Views: 89

Answers (1)

ngShravil.py
ngShravil.py

Reputation: 5058

As of now, Mongo doesn't support cascade delete operation. Mongo just does what it has been told to do. Cascade delete is not a concept in MongoDB, it's more of a concept in SQL.

So, for now what you can do is, you can manually delete the matching record in every collection. You can use the below to delete a document:

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

There is another way, but, it's a long route. What you can do is change your schema. Instead of making different collection, you can put everything in just one collection. But, I would recommend you to do this only if your collections are connected and it's frequently updating.

Upvotes: 1

Related Questions