0

I need to fetch collection list and each collection's keys from Mongodb and compare those key in loop with struct which has same name as collection name

Struct in dettails.go

type Chats struct {
    Id  string
    Value string
}

//Controller.go Fetched collection list from Mongodb

names, err := db.CollectionNames()

Loop over collection list and fetched each collection's key list Example collection chats has key Id and Value Now in the same loop I tried to fetch fields of struct (collection name is similar to struct name)

for _, name := range names {
     clKeyList := keylist(name)

     a := &models.name{} // here calling struct dynamically for every iteration but name gives error
     fmt.Println(name)

}

Please suggest some solution, working since last two days to resolve.

Piyu Upa
  • 1
  • 1
  • https://stackoverflow.com/questions/18930910/access-struct-property-by-name – Magiq Jun 14 '19 at 08:30
  • @Magiq This is not a appropriate answer. I already explained above that I have multiple struct in different models. I run a loop to collection list and on every iteration I have to call relevant struct inside for loop based on collection name. – Piyu Upa Jun 14 '19 at 09:01
  • You can define a map that maps from the name to the object you want, e.g., `map[string]interface{}`. – Andy Schweig Jun 14 '19 at 15:05

0 Answers0