drox90250557 2019-09-06 21:19
浏览 291

在Golang中使用游标进行迭代时如何从mongodb记录中提取字段

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("
result type:", reflect.TypeOf(myResult))
            fmt.Printf("result: %+v
", 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("
result type:", reflect.TypeOf(bsonMResult))
            fmt.Println("
result:", 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条回答 默认 最新

  • dongwuchu0314 2019-09-06 23:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向