Last active
August 29, 2015 14:18
-
-
Save sumitasok/7a33e5572749c9fd5bde to your computer and use it in GitHub Desktop.
Mongo Mgo Cheat Sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sess := mgoSession() | |
collection := sess.DB("db").C("collection") | |
collection.Insert(&offer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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