Golang:无法将数组解码为ObjectID

问题描述 投票:1回答:1

所以我有这个结构,当我从数据库将其解码为结构时,出现此错误cannot decode array into an ObjectID

type Student struct {
    ID           primitive.ObjectID   `bson:"_id,omitempty"`
    ...
    Hitches      []primitive.ObjectID `bson:"hitches"`
    ...
}

我正在使用此功能进行解码

func GetStudentByID(ID primitive.ObjectID) model.Student {

    // Filter
    filter := bson.M{"_id": ID}

    // Get the collection
    studentCollection := GetStudentCollection()

    // The object that it will return
    student := model.Student{}

    // Search the database
    err := studentCollection.FindOne(context.TODO(), filter).Decode(&student)

    if err != nil {
        fmt.Println("Student DAO ", err)  <----------- Error is output here
        return model.Student{}
    }

    return student
}

这里是MongoDB的屏幕截图

arrays mongodb go slice mongo-go
1个回答
2
投票
type Student struct { ID primitive.ObjectID `bson:"_id,omitempty"` ... Hitches [][]primitive.ObjectID `bson:"hitches"` ... }
© www.soinside.com 2019 - 2024. All rights reserved.