本文整理汇总了Golang中labix/org/v2/mgo.Session.DatabaseNames方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.DatabaseNames方法的具体用法?Golang Session.DatabaseNames怎么用?Golang Session.DatabaseNames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Session
的用法示例。
在下文中一共展示了Session.DatabaseNames方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: resetAdminPasswordAndFetchDBNames
// resetAdminPasswordAndFetchDBNames logs into the database with a
// plausible password and returns all the database's db names. We need
// to try several passwords because we don't know what state the mongo
// server is in when Reset is called. If the test has set a custom
// password, we're out of luck, but if they are using
// DefaultStatePassword, we can succeed.
func resetAdminPasswordAndFetchDBNames(session *mgo.Session) ([]string, bool) {
// First try with no password
dbnames, err := session.DatabaseNames()
if err == nil {
return dbnames, true
}
if !isUnauthorized(err) {
panic(err)
}
// Then try the two most likely passwords in turn.
for _, password := range []string{
DefaultMongoPassword,
utils.UserPasswordHash(DefaultMongoPassword, utils.CompatSalt),
} {
admin := session.DB("admin")
if err := admin.Login("admin", password); err != nil {
logger.Infof("failed to log in with password %q", password)
continue
}
dbnames, err := session.DatabaseNames()
if err == nil {
if err := admin.RemoveUser("admin"); err != nil {
panic(err)
}
return dbnames, true
}
if !isUnauthorized(err) {
panic(err)
}
logger.Infof("unauthorized access when getting database names; password %q", password)
}
return nil, false
}
示例2: listDbs
func listDbs(session *mgo.Session) {
dbNames, err := session.DatabaseNames()
if err != nil {
panic(err)
}
for i := 0; i < len(dbNames); i++ {
fmt.Println(dbNames[i])
}
}
示例3: dropAll
// dropAll drops all databases apart from admin, local and config.
func dropAll(session *mgo.Session) (err error) {
names, err := session.DatabaseNames()
if err != nil {
return err
}
for _, name := range names {
switch name {
case "admin", "local", "config":
default:
err = session.DB(name).DropDatabase()
if err != nil {
return err
}
}
}
return nil
}