我有一个模型,可以创建一个没有问题的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
}

struct

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 算法rithms",
        "description": "An awesome course",
        "lessons": "4",
        "duration": "10 hours",
        "course_details": {
            "title": "",
            "instructor": "",
            "introduction": "",
            "learn": "",
            "topics": "",
            "prerequisites": "",
            "goal": "",
            "additionalDetails": "",
            "highLevelOverview": ""
        }
    },
    "metadata": "none"
}

根据我所读到的内容,Decode也应该映射Nest值吗?

推荐答案

杰森?而是the Go MongoDB driver works with BSON

The struct tags are used to define how the Go struct fields should be mapped to the MongoDB document fields.
In your Course struct, you are using json tags, but the Decode method from the MongoDB driver in Go uses the bson tags to map the document fields to the struct fields.

要解决这个问题,您应该向您的 struct 字段添加bson个标记,包括嵌套的 struct ,以指示MongoDB驱动程序如何将文档解码为您的 struct .

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

Mongodb相关问答推荐

MongoDB:从集合页面数据中提取不同的值

MongoDB-如何过滤和获取数组字段中的最新数据

如何将空值单独分组?

在MongoDB Aggregate for My BooksDB中将`$match`放在`$unwin`之前或之后的区别

MongoDB:如何从数组中的所有对象中删除属性?

如何向所有文档添加一个字段,其中前 100 个文档的值为 1,接下来的 100 个文档的值为 2,依此类推?

(MongoDB)在同一管道中结合联合和交集

Raft Vs MongoDB 初选

如何将记录从一个 mongo 数据库插入另一个?

Mongoexport 在日期范围内使用 $gt 和 $lt 约束

Java MongoDB FindOne 获取最后插入的记录

MongoDB:单个数据库处理程序的 >5 个打开连接

NoSQL:MongoDB 或 BigTable 并不总是 Available意味着什么

Ruby 按键值分组哈希

Mongo按实际上是数字的字符串值排序

如何从 Mongoose 模型对象中获取集合名称

mongoose:按字母顺序排序

RoboMongo:不显示所有文档

如何在java中删除mongodb集合中的所有文档

无法连接到远程服务器上的 mongo