hi, This is what i have done so far but still need some help as to how to return ""v.APIVersionInURI" in the loop function (FindAlluris()) . Am trying to write a function which will return all the field ""v.APIVersionInURI" in the mongo db and then use this function in my API handler function. I have tried this
// The strut look like this type NfInstance struct { ID bson.ObjectId `bson:"_id" json:"id"` NfServices []Service `json:"nfServices" bson:"nfServices"` ...... } type Service struct { Versions []Version `json:"versions" bson:"versions"` ...... } type Version struct { APIVersionInURI string `json:"apiVersionInUri" bson:"apiVersionInUri"` ... } // Establish a connection to database func (m *NfInstanceDataAccess) Connect() { session, err := mgo.Dial(m.Server) if err != nil { log.Fatal(err) } db = session.DB(m.Database) } // Find list of nfinstances URIs func (m *NfInstanceDataAccess) FindAlluris() (NfInstance, error) { nfInstance := NfInstance{} itr := db.C(COLLECTION).Find(bson.M{}).Iter() for itr.Next(&nfInstance) { for _, svc := range nfInstance.NfServices { for _, v := range svc.Versions { fmt.Println(v.APIVersionInURI) } } } if err := itr.Close(); err != nil { fmt.Println(err) } } but dont know where to return all "v.APIVersionInURI" strings. The handler function look like this // Get all database URIs func NfInstanceUrlsGet(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") uris, err := da.FindAlluris() if err != nil { respondWithError(w, http.StatusInternalServerError, err.Error()) return } respondWithJson(w, http.StatusOK, uris) } Thanks in advance On Thursday, February 14, 2019 at 2:33:43 PM UTC+2, afriyie...@gmail.com wrote: > > Hi, > > Am quite new in go language and asking for help. > I have this JSON object in mongodb with collection name "COLLECTION", > I have multiple object in the database (mongo) and would like to access > all the string > values """apiVersionInUri": "string", in all the objects. > Can anyone help me do this in Golang. I know i will need a for loop to > achieve this but > i dont know how to go about it. > > Thanks in advance > > //Example JSON object look like this > > > { > "nfInstanceId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", > "heartBeatTimer": 0, > "sNssais": [ > { > "sst": 0, > "sd": "string" > } > ], > "nsiList": [ > "string" > ], > "nfServices": [ > { > "serviceInstanceId": "string", > "versions": [ > { > "apiVersionInUri": "string", > "apiFullVersion": "string", > "expiry": "2019-02-03T12:08:56.650Z" > } > ] > } > ] > } > > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.