亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 mongo-go-driver 解碼駝峰式字段

無法使用 mongo-go-driver 解碼駝峰式字段

Go
千萬里不及你 2023-06-12 12:55:20
我正在使用這樣的結構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 protected]","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 protected]",    "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}"        }    }}我不明白為什么要全部小寫?;蛘呶义e過了什么?
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

不,它不是解碼駝峰式字段失敗。它無法解析結構標簽。

根據文檔:

按照慣例,標記字符串是可選的空格分隔鍵:“值”對的串聯。每個鍵都是一個非空字符串,由除空格(U+0020 ' ')、引號(U+0022 '"')和冒號(U+003A ':')之外的非控制字符組成。每個值都被引用使用 U+0022 '"' 字符和 Go 字符串文字語法。

json:您應該刪除and之后的空格bson:。


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號