首頁 > 後端開發 > Golang > 主體

使用 mongo'd db .Decode(&dto) 來映射巢狀結構

PHPz
發布: 2024-02-09 11:51:08
轉載
1066 人瀏覽過

使用 mongo\'d db .Decode(&dto) 映射嵌套结构

php小編子墨今天為大家介紹一個使用mongo'd db .Decode(&dto)來對應巢狀結構的方法。在開發過程中,我們經常會遇到需要將嵌套結構從資料庫中解碼並映射到對應的資料傳輸物件(DTO)中的情況。 mongo'db的Decode函數可以幫助我們簡化這個過程,只需要傳入要解碼的結構和目標DTO對象,就能自動進行解碼和映射。這種方法簡單且高效,可以大大提高開發效率。接下來,我們將詳細介紹如何使用這個方法來實作巢狀結構的對應。

問題內容

我有一個模型,可以毫無問題地建立 json 文檔,但檢索它會導致嵌套的 json 物件為空。

func (r *courserepo) getcoursebyid(ctx context.context, id string) (course, error) {
    clog := log.getloggerfromcontext(ctx)

    var course course

    objid, err := primitive.objectidfromhex(id)
    if err != nil {
        return course, err
    }

    filter := bson.m{"_id": objid}

    err = r.collection.findone(ctx, filter).decode(&course)
    if err != nil {
        clog.errorctx(err, log.ctx{"msg": "an error occurred while finding a course"})

        return course, err
    }

    return course, nil
}
登入後複製

結構體

type course struct {
    objectid    primitive.objectid `bson:"_id, omitempty"`
    id          string             `json:"id"`
    title       string             `json:"title"`
    description string             `json:"description"`
    lessons     string             `json:"lessons"`
    duration    string             `json:"duration"`
    details     struct {
        title             string `json:"title"`
        instructor        string `json:"instructor"`
        introduction      string `json:"introduction"`
        learn             string `json:"learn"`
        topics            string `json:"topics"`
        prerequisites     string `json:"prerequisites"`
        goal              string `json:"goal"`
        additionaldetails string `json:"additionaldetails"`
        highleveloverview string `json:"highleveloverview"`
    } `json:"course_details"`
}
登入後複製

結果

{
    "data": {
        "ObjectId": "64953ac1bf06bfdd7936cad8",
        "id": "",
        "title": "Java Algorithms",
        "description": "An awesome course",
        "lessons": "4",
        "duration": "10 hours",
        "course_details": {
            "title": "",
            "instructor": "",
            "introduction": "",
            "learn": "",
            "topics": "",
            "prerequisites": "",
            "goal": "",
            "additionalDetails": "",
            "highLevelOverview": ""
        }
    },
    "metadata": "none"
}
登入後複製

根據我讀到的內容,decode 也應該要對應巢狀值嗎?

解決方法

json?但是 go mongodb 驅動程式可與 bson 配合使用

#結構體標籤用於定義 go 結構體欄位應如何對應到 mongodb 文件欄位。
course 結構中,您使用json 標籤,但decode 方法使用bson 標籤將文件欄位映射到結構體字段。

要解決此問題,您應該將 bson 標記新增至您的結構欄位(包括巢狀結構),以指示 mongodb 驅動程式如何將文件解碼到您的結構中。

type Course struct {
    ObjectId    primitive.ObjectID `bson:"_id,omitempty" json:"ObjectId"`
    Id          string             `bson:"id" json:"id"`
    Title       string             `bson:"title" json:"title"`
    Description string             `bson:"description" json:"description"`
    Lessons     string             `bson:"lessons" json:"lessons"`
    Duration    string             `bson:"duration" json:"duration"`
    Details     struct {
        Title             string `bson:"title" json:"title"`
        Instructor        string `bson:"instructor" json:"instructor"`
        Introduction      string `bson:"introduction" json:"introduction"`
        Learn             string `bson:"learn" json:"learn"`
        Topics            string `bson:"topics" json:"topics"`
        Prerequisites     string `bson:"prerequisites" json:"prerequisites"`
        Goal              string `bson:"goal" json:"goal"`
        AdditionalDetails string `bson:"additionalDetails" json:"additionalDetails"`
        HighLevelOverview string `bson:"highLevelOverview" json:"highLevelOverview"`
    } `bson:"course_details" json:"course_details"`
}
登入後複製

請注意,您可以在同一欄位上同時擁有 bsonjson 標籤。 bson 標籤在與mongodb 互動時使用(例如,當呼叫.decode() 時),而json 標籤在編組/解組到json 格式時使用。

此外,請確保 bson 標記中的欄位名稱與 mongodb 文件中的欄位名稱相符。例如,如果mongodb 文件中的欄位名稱為coursedetails 而不是course_details,則應將bson 標記更改為bson:"coursedetails"

以上是使用 mongo'd db .Decode(&dto) 來映射巢狀結構的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!