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"`
}
请注意,您可以在同一字段上同时拥有 bson
和 json
标签。 bson
标签在与 mongodb 交互时使用(例如,当调用 .decode()
时),而 json
标签在编组/解组到 json 格式时使用。
此外,请确保 bson
标记中的字段名称与 mongodb 文档中的字段名称匹配。例如,如果 mongodb 文档中的字段名为 coursedetails
而不是 course_details
,则应将 bson
标记更改为 bson:"coursedetails"
。