I copied the example of the find operation from the Go driver documentation. When I do I get a warning for
go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fieldsgo-vet
on the find field. I created a struct to use in the decode and the document is returned without issue. Can anyone explain what this warning is about? Here is the full function
func getMovie(c *gin.Context) {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
uri := os.Getenv("MONGODB_URI")
if uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
coll := client.Database("axelrod").Collection("movies")
title := "Back to the Future"
var movie Movie
err = coll.FindOne(context.TODO(), bson.D{{"title", title}}).Decode(&movie)
if err == mongo.ErrNoDocuments {
fmt.Printf("No document was found with the title %s\n", title)
return
}
if err != nil {
panic(err)
}
jsonData, err := json.MarshalIndent(movie, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", jsonData)
c.JSON(http.StatusOK, movie)
}