On Thu, Feb 14, 2019 at 5:33 AM <afriyie.abra...@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
There are several ways you can do this. You can unmarshal bson from the db by defining: type Obj struct { NFServices []Service `bson:"nfServices"` ... } type Service struct { Versions []Version `bson:"versions"` ... } type Version struct { APIVersionInURI string `bson:"apiVersionInUri"` } Then reading Obj from the db, and accessing the values you need: var obj Obj itr:=coll.Find(...).Iter() for itr.Next(&obj) { for _,svc:=range obj.NFServices { for _,v:=range svc.Versions { //use v.APIVersionInURI } } } You can also read it to a map, and iteratively look in it. > > //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. -- 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.