The mongo adapter for MongoDB wraps the driver written by Gustavo Niemeyer.

Known limitations

  • Does not support transactions.
  • Does not support the db tag. You must use bson tags instead.
  • Does not support query builder.


If you want to install the package with go get, you’ll need the bazaar version control system.

You can install bzr like this:

sudo apt-get install bzr -y

After bazaar is installed, use go get to download and install the adapter.

go get

Setting up database access

The mongo.ConnectionURL{} struct is defined as follows:

// ConnectionURL implements a MongoDB connection struct.
type ConnectionURL struct {
  User     string
  Password string
  Host     string
  Database string
  Options  map[string]string

Pass the mongo.ConnectionURL value as argument for mongo.Open() to create a mongo.Database session.

A mongo.ParseURL() function is provided to convert a DSN into a mongo.ConnectionURL:

// ParseURL parses s into a ConnectionURL struct.
mongo.ParseURL(s string) (ConnectionURL, error)


Import the package into your application:

// main.go
package main

import (

Then, you can use the mongo.Open() method to create a session:

var settings = mongo.ConnectionURL{
  Host:       "localhost",          // PostgreSQL server IP or name.
  Database:   "peanuts",            // Database name.
  User:       "cbrown",             // Optional user name.
  Password:   "snoopy",             // Optional user password.

sess, err = mongo.Open(settings)


The Go code below will add some rows to the “birthday” collection and then will print the same rows that were inserted.

// example.go

package main

import (


var settings = mongo.ConnectionURL{
  Database:  `upperio_tests`,
  Host:      ``,

type Birthday struct {
  // Maps the "Name" property to the "name" column
  // of the "birthday" table.
  Name string `bson:"name"`
  // Maps the "Born" property to the "born" column
  // of the "birthday" table.
  Born time.Time `bson:"born"`

func main() {

  // Attemping to establish a connection to the database.
  sess, err := mongo.Open(settings)
  if err != nil {
    log.Fatalf("db.Open(): %q\n", err)
  defer sess.Close() // Remember to close the database session.

  // Pointing to the "birthday" table.
  birthdayCollection := sess.Collection("birthday")

  err = birthdayCollection.Truncate()
  if err != nil {
    log.Fatalf("Truncate(): %q\n", err)

  // Inserting some rows into the "birthday" table.
    Name: "Hayao Miyazaki",
    Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.UTC),

    Name: "Nobuo Uematsu",
    Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.UTC),

    Name: "Hironobu Sakaguchi",
    Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.UTC),

  // Let's query for the results we've just inserted.
  res := birthdayCollection.Find()

  // Query all results and fill the birthday variable with them.
  var birthday []Birthday

  err = res.All(&birthday)
  if err != nil {
    log.Fatalf("res.All(): %q\n", err)

  // Printing to stdout.
  for _, birthday := range birthday {
      "%s was born in %s.\n",
      birthday.Born.Format("January 2, 2006"),

Running the example above:

go run main.go

Expected output:

Hayao Miyazaki was born in January 5, 1941.
Nobuo Uematsu was born in March 21, 1959.
Hironobu Sakaguchi was born in November 25, 1962.