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{}.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装