Skip to content

Instantly share code, notes, and snippets.

@novalagung
Created August 7, 2018 05:51
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 novalagung/33576ada99836d665c2eda5baf8b1462 to your computer and use it in GitHub Desktop.
Save novalagung/33576ada99836d665c2eda5baf8b1462 to your computer and use it in GitHub Desktop.
// Get session
info := new(mgo.DialInfo)
info.Username = "username"
info.Password = "password"
info.Source = "admin"
info.Addrs = []string{"host"}
info.Database = "dbname"
session, err := mgo.DialWithInfo(info)
if err != nil {
fmt.Printf("dial fail %v\n", err)
os.Exit(1)
}
defer session.Close()
// Error check on every access
session.SetSafe(&mgo.Safe{})
// Get collection
collection := session.DB("dbname").C("collectionName")
// Delete record
err = collection.Remove(bson.M{})
if err != nil {
fmt.Printf("remove fail %v\n", err)
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment