title: Inserting data into MongoDB with mgo
body: <p>I'm trying to insert some data in MongoDB using Go.</p>
  
  <p>Here is the data struct:</p>
  
  <pre><code>type Entry struct {
      Id          string `json:"id",bson:"_id,omitempty"`
      ResourceId  int    `json:"resource_id,bson:"resource_id"`
      Word        string `json:"word",bson:"word"`
      Meaning     string `json:"meaning",bson:"meaning"`
      Example     string `json:"example",bson:"example"`
  }
  </code></pre>
  
  <p>This is my insert function:</p>
  
  <pre><code>func insertEntry(db *mgo.Session, entry *Entry) error {
      c := db.DB(*mongoDB).C("entries")
      count, err := c.Find(bson.M{"resourceid": entry.ResourceId}).Limit(1).Count()
      if err != nil {
          return err
      }
      if count &gt; 0 {
          return fmt.Errorf("resource %s already exists", entry.ResourceId)
      }
      return c.Insert(entry)
  }
  </code></pre>
  
  <p>And finally, this is how I call it:</p>
  
  <pre><code>entry := &amp;Entry{
      ResourceId:  resourceId,
      Word:        word,
      Meaning:     meaning,
      Example:     example,
  }
  err = insertEntry(db, entry)
  if err != nil {
      log.Println("Could not save the entry to MongoDB:", err)
  }
  </code></pre>
  
  <p>The trouble is, I was expecting my <code>bson</code> tags to magically work, but they don't.
  Instead of data being saved as:</p>
  
  <blockquote>
    <p>{ "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "resource_id" :
    7660708, "word" : "Foo" ...}</p>
  </blockquote>
  
  <p>It gets saved as:</p>
  
  <blockquote>
    <p>{ "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "id" : "",
    "resourceid" : 7660708, "word" : "Foo"...}</p>
  </blockquote>
  
  <p>How can I fix this?</p>