1

I am fairly new to golang programming and the mongodb interface.

I've got a dbase of records created by another application. I am trying to walk the dbase and examine specific fields of each record. I can decode the full records as bson, but I cannot get the specific values.

This struct defines the 3 fields I would like to extract:

type myDbaseRec struct {
    aid        string  `bson:"pon-util-aid"`
    ingressPct string  `bson:"ingress-bucket-percent"`
    egressPct  string  `bson:"egress-bucket-percent"`
}

Here is my code to iterate through the cursor after the collection.Find(ctx, queryFilter) and decode the results both as bson and as my struct:

    var myResult myDbaseRec
    var bsonMResult bson.M

    var count int
    for cursor.Next(ctx) {

        err := cursor.Decode(&myResult)
        if err != nil {
            fmt.Println("cursor.Next() error:", err)
            panic(err)
            // If there are no cursor.Decode errors
        } else {
            fmt.Println("\nresult type:", reflect.TypeOf(myResult))
            fmt.Printf("result: %+v\n", myResult)

        }

        err = cursor.Decode(&bsonMResult)
        if err != nil {
            fmt.Println("bson decode error:", err)
            panic(err)
            // If there are no cursor.Decode errors
        } else {
            fmt.Println("\nresult type:", reflect.TypeOf(bsonMResult))
            fmt.Println("\nresult:", bsonMResult)
        }

    }

Here is an example of one iteration of the loop. The bson decode appears to work, but my struct is empty:

result type: internal.myDbaseRec
result: {aid: ingressPct: egressPct:}

result type: primitive.M

result: map[pon-util-aid:ROLT-1-MONTREAL/1/1/xp2 _id:ObjectID("5d70b4d1b3605301ef72228b") 
admitted-assured-upstream-bw:0 admitted-excess-upstream-bw:0 admitted-fixed-upstream-bw:0 
assured-upstream-bytes:0 available-excess-upstream-bw:0 available-fixed-upstream-bw:622080 
app_counters_key_field:ROLT-1-MONTREAL/1/1/xp2 app_export_time:1567665626 downstream-octets:52639862633214 
egress-bucket-bps:8940390198 egress-bucket-percent:91 egress-bucket-seconds:559 
excess-upstream-bytes:0 fixed-upstream-bytes:0 ingress-bucket-bps:8253153852 
ingress-bucket-percent:84 ingress-bucket-seconds:559 sample-time:0 upstream-octets:48549268162714]

I would have expected to get

result: {aid:"ROLT-1-MONTREAL/1/1/xp2" ingressPct:84 egressPct:91}

Any suggestion on how to properly find these 3 fields from each record?

=== UPDATE: The first comment below answered my question.

1 Answer 1

1

First, in Go only fields starting with a (Unicode) upper case letter are exported. See also Exported identifiers. The default decoder will try to decode only to the exported fields. So you should change the struct into:

type myDbaseRec struct {
    Aid        string  `bson:"pon-util-aid"`
    IngressPct int32  `bson:"ingress-bucket-percent"`
    EgressPct  int32  `bson:"egress-bucket-percent"`
}

Also notice that the struct above for IngressPct and EgressPct have type int32. This is because the value in the document is represented in numbers (int/double), and not string. You may change it to other number type accordingly i.e. int16, int64, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Changing the struct the way you suggest allows me to get the values from the database records now! This was a big help!

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.