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