我有这个结构,当我将它从数据库解码为结构时,我收到了这个错误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的截图