-2

I have data in MongoDB as tree structure model. Here is my data structure: enter image description here

Here is my Mongo shell to create collection:

db.users.insert({_id:"DHBK",username:"DHBK",password:"123456",lastname:"DHBK",user_email:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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:"[email protected]",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.

1 Answer 1

1

Here is the working Golang code, you just need to update MongoDB connection string and database name.

package main

import (
    "context"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb_connect_string"))
    if err != nil {
        panic(err)
    }
    defer client.Disconnect(ctx)

    database := client.Database("testdb")
    users := database.Collection("users")

    matchStage := bson.D{{"$match", bson.D{{"$or", bson.A{bson.D{{"_id", "BOMON_VIENTHONG"}}, bson.D{{"hierarchy._id", "BOMON_VIENTHONG"}}}}}}}
    graphStage := bson.D{{"$graphLookup", bson.D{{"from", "users"}, {"startWith", "$user_parentid"}, {"connectFromField", "user_parentid"}, {"connectToField", "_id"}, {"as", "hierarchy"} }}}

    showInfoCursor, err := users.Aggregate(ctx, mongo.Pipeline{graphStage, matchStage})
    if err != nil {
            panic(err)
    }
    var showsWithInfo []bson.M
    if err = showInfoCursor.All(ctx, &showsWithInfo); err != nil {
            panic(err)
    }

    for _, doc := range showsWithInfo {
        _, err := users.DeleteOne(ctx, bson.M{"_id": doc["_id"]})
        if (err != nil) {
            panic(err)
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello sir, can you use in mgo, please?
It's work for me, but I am using mgo in function connection, insert, update,...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.