According to the documentation in mongo-driver https://godoc.org/go.mongodb.org/mongo-driver/bson Unmarshalling bson into interface{} will default the value into D
- When unmarshalling, a field of type interface{} will follow the D/M type mappings listed above. BSON documents unmarshalled into an interface{} field will be unmarshalled as a D.
If I store an object in db as:
"data": {
"property1": "value1",
"property2": "value2",
}
When I retrieve this data with"
var result interface{}
err = collection.FindOne(ctx, filter).Decode(&result)
it becomes:
"data": [
{"Key": "property1", "Value": "value1"},
{"Key": "property2", "Value": "value2"}
]
Which match the documentation, type D is a slice of key value
My question is, is there a way to change this behavior so that it will unmarshal the data into M?
My app is not aware of the actual structure of the data. It simply takes the json as input and store in db so I have to use interface{} as the type when unmarhsal. If it stores in db as a map, it should retrieve the data in same way.