當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Session.DatabaseNames方法代碼示例

本文整理匯總了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
}
開發者ID:jkary,項目名稱:core,代碼行數:39,代碼來源:mgo.go

示例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])
	}
}
開發者ID:JanxSpirit,項目名稱:mon.go,代碼行數:9,代碼來源:mon.go

示例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
}
開發者ID:jkary,項目名稱:core,代碼行數:18,代碼來源:mgo.go


注:本文中的labix/org/v2/mgo.Session.DatabaseNames方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。