Let's say we have a Go struct like so
type Person struct {
Name string `json:"name" bson:"name"`
}
Whenever I want to search a field for this person, in the Mongo module mgo
, I do something like userCollection.Find(bson.M{"name" : "John"})
What I want to know is, is there a way to call Find
like above, but where I don't need to type out the string field that I'm looking for ("name"
in this case). I want to avoid this because if I change the bson
field name in the struct, I'll have to comb through the code base and change it everywhere.
Aside from having global constants everywhere for every struct's field, is there a way to call mgo
using the struct's own field?
For instance, something which can be called userCollection.Find(bson.M{Person.Name : "John"})
, which will automatically find Person.Name
field in the struct (which is "name"
in this case) and perform the query.