doson1998 2019-02-25 04:17
浏览 79
已采纳

无法使用mongo-go-driver解码camelcase字段

I'm using a struct like so

type User struct {
    Username  string    `json: "username" bson: "username"`
    FirstName string    `json: "firstName" bson: "firstName"`
    LastName  string    `json: "lastName" bson: "lastName"`
    Email     string    `json: "email" bson: "email"`
    Gender    string    `json: "gender" bson: "gender"`
    Password  string    `json: "password" bson: "password"`
    Enabled   bool      `json: "enabled" bson: "enabled"`
    BirthDate time.Time `json: "birthDate" bson: "birthDate"`
    CreatedAt time.Time `json: "createdAt" bson: "createdAt"`
    UpdatedAt time.Time `json: "updatedAt" bson: "updatedAt"`
    collection *mongo.Collection
}

Then query data using

func (u *User) FindByUsername(userName string) error {
    var ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
    filter := bson.M{"username": userName}
    err := u.collection.FindOne(ctx, filter).Decode(&u)
    return err
}

The result that I'm getting is

{"Username":"jbond","FirstName":"","LastName":"","Email":"email@gmail.com","Gender":"Male","Password":"","Enabled":true,"BirthDate":"0001-01-01T00:00:00Z","CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z"}

Most data are populated except for camel-case fields, and when I query from the console the data are there

> db.users.find().pretty()
{
    "_id" : ObjectId("xxxxxxxxxxxxxxxxxxxxxxxx"),
    "username" : "jbond",
    "firstName" : "James",
    "lastName" : "Bond",
    "email" : "email@gmail.com",
    "password" : "",
    "enabled" : true,
    "gender" : "Male",
    "birthDate" : {
        "type" : {
            "code" : "function Date() {
    [native code]
}"
        }
    },
    "createdAt" : {
        "type" : {
            "code" : "function Date() {
    [native code]
}"
        },
        "default" : {
            "code" : "function now() {
    [native code]
}"
        }
    },
    "updatedAt" : {
        "type" : {
            "code" : "function Date() {
    [native code]
}"
        },
        "default" : {
            "code" : "function now() {
    [native code]
}"
        }
    }
}

I don't understand why should it all be lowercase. Or am I missing something?

  • 写回答

1条回答 默认 最新

  • dongzantai7570 2019-02-25 05:07
    关注

    No, it is not failing decode camel-case fields. It is failing to parse struct tags.

    As per doc:

    By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

    You should remove the space after json: and bson:.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大
  • ¥15 使用spss做psm(倾向性评分匹配)遇到问题