使用 mongo-go-driver 解码驼峰式字段失败
Failed to decode camelcase fields using mongo-go-driver
我正在使用这样的结构
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
}
然后使用
查询数据
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
}
我得到的结果是
{"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"}
除了驼峰式字段外,大多数数据都已填充,当我从控制台查询时,数据就在那里
> 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() {\n [native code]\n}"
}
},
"createdAt" : {
"type" : {
"code" : "function Date() {\n [native code]\n}"
},
"default" : {
"code" : "function now() {\n [native code]\n}"
}
},
"updatedAt" : {
"type" : {
"code" : "function Date() {\n [native code]\n}"
},
"default" : {
"code" : "function now() {\n [native code]\n}"
}
}
}
我不明白为什么要全部小写。还是我遗漏了什么?
不,它不是解码驼峰式字段失败。无法解析结构标签。
根据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.
您应该删除 json:
和 bson:
之后的 space。
我正在使用这样的结构
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
}
然后使用
查询数据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
}
我得到的结果是
{"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"}
除了驼峰式字段外,大多数数据都已填充,当我从控制台查询时,数据就在那里
> 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() {\n [native code]\n}"
}
},
"createdAt" : {
"type" : {
"code" : "function Date() {\n [native code]\n}"
},
"default" : {
"code" : "function now() {\n [native code]\n}"
}
},
"updatedAt" : {
"type" : {
"code" : "function Date() {\n [native code]\n}"
},
"default" : {
"code" : "function now() {\n [native code]\n}"
}
}
}
我不明白为什么要全部小写。还是我遗漏了什么?
不,它不是解码驼峰式字段失败。无法解析结构标签。
根据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.
您应该删除 json:
和 bson:
之后的 space。