I'm learning how to use the community maintained mgo
repo linked here.
I've discovered through this example that the way to query for some pieces of data seems to be done through BSON (The community maintained version of the BSON library is linked here):
type Person struct {
Name string
Phone string
}
c.Find(bson.M{"name": "Alice"}).All(&result)
Which will give me all Person
with name Alice
.
I have also discovered that I don't need BSON to do this query. Find()
takes any interface:
c.Find(Person{
Name: "Alice",
Phone: "1234",
}).All(&result)
Will also work, but only returning the specified exact query. If I leave out piece of the struct like this:
c.Find(Person{
Name: "Alice",
}).All(&result)
result
will be empty.
It seems to me that avoiding BSON is more intuitive and simpler. So my questions are:
- Should I avoid the BSON import?
- Can I query for all matching patterns without using BSON or specifying the entire interface?
- Should I specify BOTH JSON and BSON keys like in
json:"name"
or are they interchangeable?