Golang: MongoDB Double Type cannot decode into Float64

Viewed 86

Currently running into an issue where the mongodb double type cannot decode into a float64 and its saying that its a string This is the error:

2022/08/02 14:24:02 error decoding key imdb.rating: cannot decode string into a float32 or float64 type

and this is what the imdb struct looks like:

    type Imdb struct {
    Rating float64 `bson:"rating,truncate"`
    Votes  int32   `bson:"votes,omitempty"`
    Id     int32   `bson:"id,omitempty"`
}

this is part of a bigger struct called movie as shown here:

type Movie struct {
    Id           primitive.ObjectID `bson:"_id,omitempty"`
    Plot         string             `bson:"plot,omitempty"`
    Genres       []string           `bson:"genres,omitempty"`
    Runtime      int32              `bson:"runtime,omitempty"`
    Rated        string             `bson:"rated,omitempty"`
    Title        string             `bson:"title,omitempty"`
    Poster       string             `bson:"poster,omitempty"`
    Countries    []string           `bson:"countries,omitempty"`
    FullPlot     string             `bson:"fullplot,omitempty"`
    CommentCount int32              `bson:"num_mflix_comments,omitempty"`
    Cast         []string           `bson:"cast,omitempty"`
    Directors    []string           `bson:"directors,omitempty"`
    Writers      []string           `bson:"writers,omitempty"`
    Awards       Award              `bson:"awards,omitempty"`
    Updated      string             `bson:"lastupdated,omitempty"`
    Year         int32              `bson:"year,omitempty"`
    Imdb         Imdb               `bson:"imdb,omitempty"`
    Type         string             `bson:"type,omitempty"`
    Tomatoes     Tomato             `bson:"tomatoes,omitempty"`
}

Example MongoDB Document

func GetMovies() []Movie {
    var movies []Movie
    db := config.GetDb()
    movieCollection := db.Collection("movies")
    curr, err := movieCollection.Find(context.Background(), bson.M{})
    if err != nil {
        log.Fatal(err)
    }
    if err = curr.All(context.Background(), &movies); err != nil {
        log.Fatal(err)
    }
    return movies
}
0 Answers
Related