title: Create a custom ID with Mgo
body: <p>I'm currently starting with GoLang and MongoDB.I'm writing a small web application, a blog to be more specific (which is like the first webapp I write when I try new languages). Everything works fine with MGO even if I had some troubles at first. But now I'd like to access each blog entry (articles will be referred as entries to stick with my models) separately. I could use the ObjectID in the url. But that's damn ugly. For example :<br>
  <code>mydomain.com/entries/543fd8940e82533995000002/</code><br>
  That's not user friendly. I did a lot of research on the internet to find a suitable solution, because using any other database engine I could just use the id (and that would be fine).</p>
  
  <p>Could someone help me with the creation of a custom (public) id which would auto-increment when I insert a new entry and that I could use in the url ?</p>
  
  <p>Here is the code of my model for now :</p>
  
  <pre><code>package models
  
  import (
      "time"
  
      "labix.org/v2/mgo"
      "labix.org/v2/mgo/bson"
  )
  
  type (
      Entries []Entry
      Entry   struct {
          ID      bson.ObjectId `bson:"_id,omitempty"`
          Title   string        `bson:"title"`
          Short   string        `bson:"short"`
          Content string        `bson:"content"`
          Posted  time.Time     `bson:"posted"`
      }
  )
  
  // Insert an entry to the database
  func InsertEntry(database *mgo.Database, entry *Entry) error {
      entry.ID = bson.NewObjectId()
      return database.C("entries").Insert(entry)
  }
  
  // Find an entry by id
  func GetEntryByID(database *mgo.Database, id string) (entry Entry, err error) {
      bid := bson.ObjectIdHex(id)
      err = database.C("entries").FindId(bid).One(&amp;entry)
      return
  }
  
  // Retrieves all the entries
  func AllEntries(database *mgo.Database) (entries Entries, err error) {
      err = database.C("entries").Find(nil).All(&amp;entries)
      return
  }
  
  // Retrieve all the entries sorted by date.
  func AllEntriesByDate(database *mgo.Database) (entries Entries, err error) {
      err = database.C("entries").Find(nil).Sort("-posted").All(&amp;entries)
      return
  }
  
  // Counts all the entries.
  func CountAllEntries(database *mgo.Database) (count int, err error) {
      count, err = database.C("entries").Find(nil).Count()
      return
  }
  </code></pre>