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:.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效