Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deserialize output from mongo-go-driver as bson array into a []interface{} when primitive.A is bson []interface

Tags:

mongodb

go

bson

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?

like image 382
ShapelyOwl Avatar asked Apr 26 '19 15:04

ShapelyOwl


1 Answers

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{}.
like image 149
mkopriva Avatar answered Oct 19 '22 06:10

mkopriva