Browse Source

drop databases

master
arnaucode 6 years ago
commit
6318845189
5 changed files with 74 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +8
    -0
      README.md
  3. +9
    -0
      errors.go
  4. +56
    -0
      main.go
  5. BIN
      rmMongodbDatabases

+ 1
- 0
.gitignore

@ -0,0 +1 @@
*.exe

+ 8
- 0
README.md

@ -0,0 +1,8 @@
# rmMongodbDatabases
simple Go code to drop databases in mongodb
usage:
```
./rmMongodbDatabases databasename1 databasename2 databasename3
```

+ 9
- 0
errors.go

@ -0,0 +1,9 @@
package main
import "log"
func check(err error) {
if err != nil {
log.Println(err)
}
}

+ 56
- 0
main.go

@ -0,0 +1,56 @@
package main
import (
"os"
"github.com/fatih/color"
mgo "gopkg.in/mgo.v2"
)
const mongoip = "127.0.0.1:27017"
func main() {
session, err := getSession()
check(err)
var databases []string
if len(os.Args) > 1 {
for i, arg := range os.Args {
if i > 0 {
databases = append(databases, arg)
}
}
}
for _, database := range databases {
db := getDatabase(session, database)
color.Yellow("delete database: " + database)
err := db.DropDatabase()
check(err)
}
}
func getSession() (*mgo.Session, error) {
session, err := mgo.Dial("mongodb://" + mongoip)
if err != nil {
panic(err)
}
//defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
return session, err
}
func getDatabase(session *mgo.Session, database string) *mgo.Database {
D := session.DB(database)
return D
}
func getCollection(session *mgo.Session, database string, collection string) *mgo.Collection {
c := session.DB(database).C(collection)
return c
}

BIN
rmMongodbDatabases


Loading…
Cancel
Save