I have data in MongoDB as tree structure model. Here is my data structure:
Here is my mongo shell to create collection:
db.users.insert({_id:"DHBK",username:"DHBK",password:"123456",lastname:"DHBK",user_email:"dhbk@edu.com.vn",user_tel:"0907111001",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:null,com_id:"DHBK",com_department:"DHBK",user_code:"DHBK_0001",user_type:"ADMIN_COM"});
db.users.insert({_id:"KHOA_DIEN",username:"KHOA_DIEN",password:"123456",lastname:"KHOA_DIEN",user_email:"KHOA_DIEN@edu.com.vn",user_tel:"0907111002",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"DHBK",com_id:"DHBK",com_department:"KHOA_DIEN",user_code:"DHBK_0002",user_type:"USER_COM"});
db.users.insert({_id:"KHOA_XD",username:"KHOA_XD",password:"123456",lastname:"KHOA_XD",user_email:"KHOA_XD@edu.com.vn",user_tel:"0907111003",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"DHBK",com_id:"DHBK",com_department:"KHOA_XD",user_code:"DHBK_0003",user_type:"USER_COM"});
db.users.insert({_id:"KHOA_CNTT",username:"KHOA_CNTT",password:"123456",lastname:"KHOA_CNTT",user_email:"KHOA_CNTT@edu.com.vn",user_tel:"0907111004",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"DHBK",com_id:"DHBK",com_department:"KHOA_CNTT",user_code:"DHBK_0004",user_type:"USER_COM"});
db.users.insert({_id:"BOMON_TUDONG",username:"BOMON_TUDONG",password:"123456",lastname:"BOMON_TUDONG",user_email:"BOMON_TUDONG@edu.com.vn",user_tel:"0907111005",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_DIEN",com_id:"DHBK",com_department:"KHOA_DIEN",user_code:"DHBK_0005",user_type:"USER_COM"});
db.users.insert({_id:"BOMON_VIENTHONG",username:"BOMON_VIENTHONG",password:"123456",lastname:"BOMON_VIENTHONG",user_email:"BOMON_VIENTHONG@edu.com.vn",user_tel:"0907111006",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_DIEN",com_id:"DHBK",com_department:"KHOA_DIEN",user_code:"DHBK_0006",user_type:"USER_COM"});
db.users.insert({_id:"BOMON_HETHONG",username:"BOMON_HETHONG",password:"123456",lastname:"BOMON_HETHONG",user_email:"BOMON_HETHONG@edu.com.vn",user_tel:"0907111007",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_DIEN",com_id:"DHBK",com_department:"KHOA_DIEN",user_code:"DHBK_0007",user_type:"USER_COM"});
db.users.insert({_id:"BOMON1_XD",username:"BOMON1_XD",password:"123456",lastname:"BOMON1_XD",user_email:"BOMON1_XD@edu.com.vn",user_tel:"0907111008",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_XD",com_id:"DHBK",com_department:"KHOA_XD",user_code:"DHBK_0008",user_type:"USER_COM"});
db.users.insert({_id:"BOMON2_XD",username:"BOMON2_XD",password:"123456",lastname:"BOMON2_XD",user_email:"BOMON2_XD@edu.com.vn",user_tel:"0907111009",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_XD",com_id:"DHBK",com_department:"KHOA_XD",user_code:"DHBK_0009",user_type:"USER_COM"});
db.users.insert({_id:"BOMON3_XD",username:"BOMON3_XD",password:"123456",lastname:"BOMON3_XD",user_email:"BOMON3_XD@edu.com.vn",user_tel:"0907111010",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"KHOA_XD",com_id:"DHBK",com_department:"KHOA_XD",user_code:"DHBK_0010",user_type:"USER_COM"});
db.users.insert({_id:"TRUONGKHOA_BMVT",username:"TRUONGKHOA_BMVT",password:"123456",lastname:"TRUONGKHOA_BMVT",user_email:"TRUONGKHOA_BMVT@edu.com.vn",user_tel:"0907111011",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"BOMON_VIENTHONG",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0011",user_type:"USER_COM"});
db.users.insert({_id:"PHOKHOA_BMVT",username:"PHOKHOA_BMVT",password:"123456",lastname:"PHOKHOA_BMVT",user_email:"PHOKHOA_BMVT@edu.com.vn",user_tel:"0907111012",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"BOMON_VIENTHONG",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0012",user_type:"USER_COM"});
db.users.insert({_id:"THUKY_BMVT",username:"THUKY_BMVT",password:"123456",lastname:"THUKY_BMVT",user_email:"THUKY_BMVT@edu.com.vn",user_tel:"0907111013",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"BOMON_VIENTHONG",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0013",user_type:"USER_COM"});
db.users.insert({_id:"GV_BMVT",username:"GV_BMVT",password:"123456",lastname:"GV_BMVT",user_email:"GV_BMVT@edu.com.vn",user_tel:"0907111014",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"BOMON_VIENTHONG",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0014",user_type:"USER_COM"});
db.users.insert({_id:"SV1_BMVT",username:"SV1_BMVT",password:"123456",lastname:"SV1_BMVT",user_email:"SV1_BMVT@edu.com.vn",user_tel:"0907111015",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"GV_BMVT",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0015",user_type:"USER_COM"});
db.users.insert({_id:"SV2_BMVT",username:"SV2_BMVT",password:"123456",lastname:"SV2_BMVT",user_email:"SV2_BMVT@edu.com.vn",user_tel:"0907111016",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"GV_BMVT",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0016",user_type:"USER_COM"});
db.users.insert({_id:"SV3_BMVT",username:"SV3_BMVT",password:"123456",lastname:"SV3_BMVT",user_email:"SV3_BMVT@edu.com.vn",user_tel:"0907111017",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"GV_BMVT",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0017",user_type:"USER_COM"});
db.users.insert({_id:"SV4_BMVT",username:"SV4_BMVT",password:"123456",lastname:"SV4_BMVT",user_email:"SV4_BMVT@edu.com.vn",user_tel:"0907111018",user_date:"2020-05-05",user_status:"ACTIVE",user_parentid:"GV_BMVT",com_id:"DHBK",com_department:"BOMON_VIENTHONG",user_code:"DHBK_0018",user_type:"USER_COM"});
In my practices, I want to remove Node parent means delete all Node Descendants.
For example:
When I remove " BOMON_VIENTHONG " node, it must remove TRUONGKHOA_BMVT, PHOKHOA_BMVT, THUKY_BMVT , GV_BMVT, SV1_BMVT, SV2_BMVT, SV3_BMVT, SV4_BMVT.
I can use mongo shell to execute this remove action. Here is my mongo shell
TOP_LEVEL_PARENT = "BOMON_VIENTHONG"
db.users.aggregate( [
{
$graphLookup: {
from: "users",
startWith: "$user_parentid",
connectFromField: "user_parentid",
connectToField: "_id",
as: "hierarchy"
}
},
{
$match: {
$or: [
{ "hierarchy._id": TOP_LEVEL_PARENT },
{ _id: TOP_LEVEL_PARENT }
]
}
}
]
).forEach( doc => db.users.deleteOne( { _id: doc._id } ) )
I want implement this mongo shell in Go. I am beginner in Go.
Please help me
Thank you in advance.