In this recipe, we will learn how we can create a BSON document (a binary-encoded serialization of JSON-like documents) in a database, using a MongoDB driver for Go (gopkg.in/mgo.v2).
Creating your first document in MongoDB
How to do it...
- Install the gopkg.in/mgo.v2 and github.com/gorilla/mux packages, using the go get command, as follows:
$ go get gopkg.in/mgo.v2
$ go get github.com/gorilla/mux
- Create create-record-mongodb.go. Then we connect to the MongoDB database, create an employee document with two fields—ID and name—and write the last created document ID to an HTTP response stream, as follows:
package main
import
(
"fmt"
"log"
"net/http"
"strconv"
"...