The update function of mongo-go-driver can be called like this.
filter := bson.D{"username", username}
update := bson.D{{"$set",
bson.D{
{"name", person.Name},
},
}}
result, err := collection.UpdateOne(ctx, filter, update)
type Person struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username,omitempty" bson:"username,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
}
But, I need to call the update function using the person
struct, without mentioning every field of person struct like this.
filter := bson.D{"username", username}
update := bson.D{{"$set", <<how to convert person struct to bson document?>>}}
result, err := collection.UpdateOne(ctx, filter, update)
How can I convert the person struct to bson document?