根据 mongo-driver https://godoc.org/go.mongodb.org/mongo-driver/bson中的文档 ,将 bson 解组到 interface{} 将默认值放入 D
如果我将一个对象存储在数据库中:
"data": {
"property1": "value1",
"property2": "value2",
}
Run Code Online (Sandbox Code Playgroud)
当我用“检索这些数据时
var result interface{}
err = collection.FindOne(ctx, filter).Decode(&result)
Run Code Online (Sandbox Code Playgroud)
它成为了:
"data": [
{"Key": "property1", "Value": "value1"},
{"Key": "property2", "Value": "value2"}
]
Run Code Online (Sandbox Code Playgroud)
与文档相符,类型D是键值的切片
我的问题是,有没有办法改变这种行为,以便它将数据解组到 M 中?
我的应用程序不知道数据的实际结构。它只是将 json 作为输入并存储在数据库中,因此我必须在 unmarhsal 时使用 interface{} 作为类型。如果它作为映射存储在数据库中,则应该以相同的方式检索数据。