dsm17496 2018-08-26 08:30 采纳率: 100%
浏览 173
已采纳

使用mongodb go驱动程序将文档解码为具有自定义类型字段的结构

I'm a beginner in both go and mongodb. I try to decode a DocumentResult into a struct using bson tags, and it does not work for a custom type wrapping a string. Can it be done without changing the field's type to a string?

    import (
    "context"
    "github.com/mongodb/mongo-go-driver/mongo"
)

type MyDoc struct {
    SomeInt int `bson:"some_int"`
    SomeString string `bson:"some_string,omitempty"`
    CustomType MyType `bson:"custom_type,omitempty"`
}

type MyType string

const myType MyType = "ABCD"

func main() {

    //Connect to db
    client, _ := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil)
    db := client.Database("example_db")
    collection := db.Collection("col")

    //Insert document
    docToInsert := MyDoc{42, "The Answer", myType}
    collection.InsertOne(nil, docToInsert)

    //Retrieve document
    filterDoc := MyDoc{SomeInt: 42}
    resultDoc := &MyDoc{}
    result := collection.FindOne(nil, filterDoc)
    result.Decode(resultDoc)

    println(resultDoc.SomeInt, resultDoc.SomeString, resultDoc.CustomType)

PRINTED RESULT: "42 The Answer" //"ABCD" is missing

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douyin7416 2018-08-27 08:56
    关注

    Unfortunately you're out of luck. The current state of the official mongo go driver does not support unmarshaling string values from BSON to a Go value whose type is a custom type having string as its underlying type. This may change in the future, but for now this is not supported.

    The way decoding into a struct field is handled is implemented in bson/decode.go, currently line #387:

    case 0x2:
        str := v.StringValue()
        switch containerType {
        case tString, tEmpty:
            val = reflect.ValueOf(str)
        case tJSONNumber:
            _, err := strconv.ParseFloat(str, 64)
            if err != nil {
                return val, err
            }
            val = reflect.ValueOf(str).Convert(tJSONNumber)
    
        case tURL:
            u, err := url.Parse(str)
            if err != nil {
                return val, err
            }
            val = reflect.ValueOf(u).Elem()
        default:
            return val, nil
        }
    

    0x02 is the BSON string type. It is only attempted to decode into the struct field if the struct field's type is any of the following: string, interface{}, json.Number or url.URL (or a pointer to these).

    Unfortunately implementing bson.Unmarshaler on your custom type does not help either, as it is not checked in case of struct fields, only if the struct itself implements it. But implementing on the struct itself, you would have to duplicate the struct with the field being one of the above listed supported types (or use a map or a bson.Document type).

    This is a serious limitation on the library's part which can very easily be solved, so let's hope for the best that they add support for this in the near future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配