Skip to content

Instantly share code, notes, and snippets.

@sumitasok
Last active August 29, 2015 14:18
Show Gist options
  • Save sumitasok/7a33e5572749c9fd5bde to your computer and use it in GitHub Desktop.
Save sumitasok/7a33e5572749c9fd5bde to your computer and use it in GitHub Desktop.
Mongo Mgo Cheat Sheet
func mgoSession() *mgo.Session {
uri := "127.0.0.1"
if uri == "" {
fmt.Println("no connection string provided")
os.Exit(1)
}
sess, err := mgo.Dial(uri)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
// defer sess.Close()
return sess
}
sess := mgoSession()
collection := sess.DB("db").C("collection")
collection.Insert(&offer)
sess := mgoSession()
sess.DB("db").C("collection").
Find(bson.M{"items": bson.M{"$elemMatch": bson.M{"name": item}}}).
All(&oL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment