douxuexiao1748
2019-04-26 15:12
浏览 918

当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?

图片转代码服务由CSDN问答提供 功能建议

我有一个map [string] interface {},它是通过使用新mongo-go的mongo查询生成的 -driver

我要处理地图中的某些值并替换属于聚合键的值中的£字符

这里是地图 :

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

遍历地图:

 输入密钥,值:=范围result2 {\  n fmt.Println(“结果2的键:”,键,“ ||”,“结果2的值,”,值)
如果键==“聚合” {
 fmt.Println(“ FOUND AGGREGATE ||  “,” value:“,value,” || type:“,reflect.TypeOf(value))
} 
 if valueMSI,确定:= value。([] interface {});  ok {
 fmt.Println(“正在工作”,valueMSI)
 fmt.Println(reflect.TypeOf(valueMSI))
} 
} 
   
 
 

现在,在检查聚合键的if语句中,第一个print语句的输出给出的类型为:

  primitive.A 
   
 
 

但是打印时似乎是地图的[]界面{}? [请参见result2]

请记住,为什么第二个if语句不被评估?

是指原始数组!==接口数组?

在文档 https://godoc.org/go.mongodb.org/mongo-driver/bson/primitive 类型A定义为“一个A代表BSON数组。该类型可以 用于简洁明了地表示BSON数组。序列化为BSON时通常应使用它。要反序列化,应使用RawArray或Array类型。“

如何 我做这个吗? 我要访问聚合键的值吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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{}.
    点赞 评论

相关推荐 更多相似问题