mirror of
https://github.com/arnaucube/rmMongodbDatabases.git
synced 2026-02-06 19:26:46 +01:00
drop databases
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.exe
|
||||
8
README.md
Normal file
8
README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# rmMongodbDatabases
|
||||
|
||||
simple Go code to drop databases in mongodb
|
||||
|
||||
usage:
|
||||
```
|
||||
./rmMongodbDatabases databasename1 databasename2 databasename3
|
||||
```
|
||||
9
errors.go
Normal file
9
errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
56
main.go
Normal file
56
main.go
Normal file
@@ -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
Executable file
BIN
rmMongodbDatabases
Executable file
Binary file not shown.
Reference in New Issue
Block a user