title: bson.M to struct
body: <p>I have a fairly odd question that I have been trying to wrap my head around and am looking of some pointers as to the best approach. I am use mgo to filter a collection that contains a few different types of structs and am trying to cast from bson.M to the proper struct after the fact. Basically I'd like to be able to filter the collection and look at each result and based on a common field cast to the proper struct.</p>
  
  <p>Here is sample of the structs I am trying to use.</p>
  
  <pre><code>  type Action interface {
      MyFunc() bool
    }
  
    type Struct1 struct {
      Id bson.ObjectId `bson:"_id,omitempty"`
      Type  string `bson:"type"`
      Struct1Only string `bson:"struct1only"`
    }
  
    func (s Struct1) MyFunc() bool {
      return true
    }
  
    type Struct2 struct {
      Id bson.ObjectId `bson:"_id,omitempty"`
      Type string `bson:"type"`
      Struct2Only string `bson:"struct2only"`
    }
  
    func (s Struct2) MyFunc() bool {
      return true
    }
  </code></pre>
  
  <p>My initial idea was to do something like:</p>
  
  <pre><code>var result bson.M{}
  err := c.Find(bson.M{nil}).One(&amp;result)
  </code></pre>
  
  <p>Then switch on the type field and cast to the proper struct, but honestly I am new to go and mongo and am sure there is better way to do this. Any suggestions? Thanks</p>