Skip to content

Instantly share code, notes, and snippets.

@larryli
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larryli/9507417 to your computer and use it in GitHub Desktop.
Save larryli/9507417 to your computer and use it in GitHub Desktop.
labix.org/v2/mgo test
package main
import (
"fmt"
. "gist.github.com/9521299.git"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
func main() {
s, err := mgo.Dial("localhost")
Die(err, "mgo.Dial")
defer s.Close()
c := s.DB("test").C("data")
var result bson.M
err = c.Find(bson.M{"name": "larry"}).One(&result)
if err == mgo.ErrNotFound {
result = bson.M{"name": "larry", "title": "Larry Li"}
err = c.Insert(result)
Die(err, "c.Insert")
fmt.Println("Write", result["name"], result["title"])
} else {
Die(err, "c.Find")
fmt.Println("Read", result["name"], result["title"])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment