You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

6 years ago
  1. package main
  2. import (
  3. "os"
  4. "github.com/fatih/color"
  5. mgo "gopkg.in/mgo.v2"
  6. )
  7. const mongoip = "127.0.0.1:27017"
  8. func main() {
  9. session, err := getSession()
  10. check(err)
  11. var databases []string
  12. if len(os.Args) > 1 {
  13. for i, arg := range os.Args {
  14. if i > 0 {
  15. databases = append(databases, arg)
  16. }
  17. }
  18. }
  19. for _, database := range databases {
  20. db := getDatabase(session, database)
  21. color.Yellow("delete database: " + database)
  22. err := db.DropDatabase()
  23. check(err)
  24. }
  25. }
  26. func getSession() (*mgo.Session, error) {
  27. session, err := mgo.Dial("mongodb://" + mongoip)
  28. if err != nil {
  29. panic(err)
  30. }
  31. //defer session.Close()
  32. // Optional. Switch the session to a monotonic behavior.
  33. session.SetMode(mgo.Monotonic, true)
  34. // Optional. Switch the session to a monotonic behavior.
  35. session.SetMode(mgo.Monotonic, true)
  36. return session, err
  37. }
  38. func getDatabase(session *mgo.Session, database string) *mgo.Database {
  39. D := session.DB(database)
  40. return D
  41. }
  42. func getCollection(session *mgo.Session, database string, collection string) *mgo.Collection {
  43. c := session.DB(database).C(collection)
  44. return c
  45. }