douxuexiao1748 2019-04-26 15:12
浏览 1772
已采纳

当primitive.A是bson [] interface时,如何将mongo-go-driver的输出作为bson数组反序列化为[] interface {}

I have a map[string]interface{} that generated from a mongo query that is using the new mongo-go-driver

I want to process certain values in the map and replace the £ characters in the values belonging to the aggregate key

Here is the map:

result2 = map[aggregate:[map[£match:map[Source:Cities]] map[£sort:map[Order:1]]] collection:aggregate_stats db:stats]

Looping through the map:

    for key, value := range result2 {
        fmt.Println("key from result2:", key, " || ", "value from result 2:", value)
        if key == "aggregate" {
            fmt.Println("FOUND AGGREGATE || ", "value:", value, " || type: ", reflect.TypeOf(value))
        }
        if valueMSI, ok := value.([]interface{}); ok {
            fmt.Println("Working", valueMSI)
            fmt.Println(reflect.TypeOf(valueMSI))
        }
    }

Now, in the if statement checking for the aggregate key, the output of the first print statement gives the type as:

primitive.A

But it appears to be an []interface{} of maps when printed? [see result2]

Bearing that in mind, why isn't the second if statement evaluated?

Does this mean that primitive.A != array of interfaces?

In the documentation https://godoc.org/go.mongodb.org/mongo-driver/bson/primitive type A is defined as "An A represents a BSON array. This type can be used to represent a BSON array in a concise and readable manner. It should generally be used when serializing to BSON. For deserializing, the RawArray or Array types should be used."

How can I do this? I want to access the values for the aggregate key?

  • 写回答

1条回答 默认 最新

  • douzao9845 2019-04-26 16:25
    关注

    You can convert a value of type primitive.A to []interface{} by using the conversion expression, the form of which is T(x).

    So in your case you can do this:

    for key, value := range result2 {
        fmt.Println("key from result2:", key, " || ", "value from result 2:", value)
        if key == "aggregate" {
            fmt.Println("FOUND AGGREGATE || ", "value:", value, " || type: ", reflect.TypeOf(value))
        }
        if pa, ok := value.(primitive.A); ok {
            valueMSI := []interface{}(pa)
            fmt.Println("Working", valueMSI)
            fmt.Println(reflect.TypeOf(valueMSI))
        }
    }
    

    As explained in the documentation you can convert a non-constant value x to type T in any of these cases (I've added emphasis for the case relevant to your question):

    • x is assignable to T.
    • ignoring struct tags (see below), x's type and T have identical underlying types.
    • ignoring struct tags (see below), x's type and T are pointer types that are not defined types, and their pointer base types have identical underlying types.
    • x's type and T are both integer or floating point types.
    • x's type and T are both complex types.
    • x is an integer or a slice of bytes or runes and T is a string type.
    • x is a string and T is a slice of bytes or runes.

    A bit on underlying types (emphasis added):

    Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration.

    Since primitive.A is defined using the type literal []interface{} it has the same underlying type as []interface{}.

    • The underlying type of []interface{} is []interface{}.
    • The underlying type of primitive.A is []interface{}.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)