How to do it...

  1. Install the gopkg.in/mgo.v2, gopkg.in/mgo.v2/bson, and github.com/gorilla/mux packages, using the go get command, as follows:
$ go get gopkg.in/mgo.v2$ go get gopkg.in/mgo.v2/bson$ go get github.com/gorilla/mux
  1. Create update-record-mongodb.go. Then we connect to the MongoDB database, update the name of an employee for an ID, and write the number of records updated in MongoDB to an HTTP response stream, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "strconv"  "github.com/gorilla/mux"  mgo "gopkg.in/mgo.v2"  "gopkg.in/mgo.v2/bson")const (  CONN_HOST = "localhost"  CONN_PORT = "8080"  MONGO_DB_URL = "127.0.0.1")var session *mgo.Sessionvar connectionError errortype Employee struct {  Id int `json:"uid"` Name string `json:"name"` ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.