本文整理汇总了Golang中launchpad/net/mgo.Dial函数的典型用法代码示例。如果您正苦于以下问题:Golang Dial函数的具体用法?Golang Dial怎么用?Golang Dial使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Dial函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: init
func init() {
initialSession = make(map[string]*mgo.Session)
// Dial to Tagesanzeiger
var session, err = mgo.Dial(TagiUrl)
if err != nil {
log.Fatal(err)
}
session.DB("tagi").Login(MongoUser, MongoPassword)
initialSession["tagi"] = session
// Dial to blick
session, err = mgo.Dial(BlickUrl)
if err != nil {
log.Fatal(err)
}
session.DB("blick").Login(MongoUser, MongoPassword)
initialSession["blick"] = session
// Dial to 20 Minuten
session, err = mgo.Dial(MinutenUrl)
session.DB("min20").Login(MongoUser, MongoPassword)
initialSession["min20"] = session
}
示例2: TestGridFSRemove
func (s *S) TestGridFSRemove(c *C) {
session, err := mgo.Dial("localhost:40011")
c.Assert(err, IsNil)
defer session.Close()
db := session.DB("mydb")
gfs := db.GridFS("fs")
file, err := gfs.Create("myfile.txt")
c.Assert(err, IsNil)
file.Write([]byte{'1'})
file.Close()
file, err = gfs.Create("myfile.txt")
c.Assert(err, IsNil)
file.Write([]byte{'2'})
file.Close()
err = gfs.Remove("myfile.txt")
c.Assert(err, IsNil)
_, err = gfs.Open("myfile.txt")
c.Assert(err == mgo.NotFound, Equals, true)
n, err := db.C("fs.chunks").Find(nil).Count()
c.Assert(err, IsNil)
c.Assert(n, Equals, 0)
}
示例3: TestAuthLoginSwitchUser
func (s *S) TestAuthLoginSwitchUser(c *C) {
session, err := mgo.Dial("localhost:40002")
c.Assert(err, IsNil)
defer session.Close()
admindb := session.DB("admin")
err = admindb.Login("root", "rapadura")
c.Assert(err, IsNil)
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1})
c.Assert(err, IsNil)
err = admindb.Login("reader", "rapadura")
c.Assert(err, IsNil)
// Can't write.
err = coll.Insert(M{"n": 1})
c.Assert(err, ErrorMatches, "unauthorized")
// But can read.
result := struct{ N int }{}
err = coll.Find(nil).One(&result)
c.Assert(err, IsNil)
c.Assert(result.N, Equals, 1)
}
示例4: main
func main() {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
err = c.Insert(&Person{"ole", "+55 53 8116 9639", map[string]Interval{"1": Interval{time.Now()}}},
&Person{"Cla", "+55 53 8402 8510", map[string]Interval{"1": Interval{time.Now()}}})
if err != nil {
panic(err)
}
result := Person{}
err = c.Find(bson.M{"name": "ole"}).One(&result)
if err != nil {
panic(err)
}
fmt.Println("Phone:", result.Other["1"].Now.Month())
}
示例5: Init
func Init() (err error) {
login := ""
if Config.User != "" {
login = Config.User + ":" + Config.Password + "@"
}
host := "localhost"
if Config.Host != "" {
host = Config.Host
}
// http://goneat.org/pkg/launchpad.net/mgo/#Session.Mongo
// [mongodb://][user:[email protected]]host1[:port1][,host2[:port2],...][/database][?options]
url := fmt.Sprintf("mongodb://%s%s/%s", login, host, Config.Database)
session, err := mgo.Dial(url)
if err != nil {
return err
}
session.SetSafe(&Config.Safe)
Database = session.DB(Config.Database)
for _, collection := range collections {
collection.collection = Database.C(collection.Name)
}
return nil
}
示例6: InitMongo
// Must initialize the mongo session
func InitMongo(url string) (err error) {
mongolog := log.New(os.Stderr, "MONGO ", log.LstdFlags)
mgo.SetLogger(mongolog)
mgo.SetDebug(true)
session, err = mgo.Dial(url)
return err
}
示例7: main
func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
if err != nil {
panic(err)
}
result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil {
panic(err)
}
fmt.Println("Phone:", result.Phone)
}
示例8: TestAuthAddUserReplaces
func (s *S) TestAuthAddUserReplaces(c *C) {
session, err := mgo.Dial("localhost:40002")
c.Assert(err, IsNil)
defer session.Close()
admindb := session.DB("admin")
err = admindb.Login("root", "rapadura")
c.Assert(err, IsNil)
mydb := session.DB("mydb")
err = mydb.AddUser("myuser", "myoldpass", false)
c.Assert(err, IsNil)
err = mydb.AddUser("myuser", "mynewpass", true)
c.Assert(err, IsNil)
admindb.Logout()
err = mydb.Login("myuser", "myoldpass")
c.Assert(err, ErrorMatches, "auth fails")
err = mydb.Login("myuser", "mynewpass")
c.Assert(err, IsNil)
// ReadOnly flag was changed too.
err = mydb.C("mycoll").Insert(M{"n": 1})
c.Assert(err, ErrorMatches, "unauthorized")
}
示例9: TestPrimaryShutdownEventual
func (s *S) TestPrimaryShutdownEventual(c *C) {
if *fast {
c.Skip("-fast")
}
session, err := mgo.Dial("localhost:40021")
c.Assert(err, IsNil)
defer session.Close()
result := &struct{ Host string }{}
err = session.Run("serverStatus", result)
c.Assert(err, IsNil)
master := result.Host
session.SetMode(mgo.Eventual, true)
// Should connect to the master when needed.
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"a": 1})
c.Assert(err, IsNil)
// Kill the master.
s.Stop(master)
// Should still work, with the new master now.
coll = session.DB("mydb").C("mycoll")
err = coll.Insert(M{"a": 1})
c.Assert(err, IsNil)
err = session.Run("serverStatus", result)
c.Assert(err, IsNil)
c.Assert(result.Host, Not(Equals), master)
}
示例10: TestSetModeMonotonicAfterStrong
func (s *S) TestSetModeMonotonicAfterStrong(c *C) {
// Test that a strong session shifting to a monotonic
// one preserves the socket untouched.
session, err := mgo.Dial("localhost:40012")
c.Assert(err, IsNil)
defer session.Close()
// Insert something to force a connection to the master.
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"a": 1})
c.Assert(err, IsNil)
session.SetMode(mgo.Monotonic, false)
// Wait since the sync also uses sockets.
for len(session.LiveServers()) != 3 {
c.Log("Waiting for cluster sync to finish...")
time.Sleep(5e8)
}
// Master socket should still be reserved.
stats := mgo.GetStats()
c.Assert(stats.SocketsInUse, Equals, 1)
// Confirm it's the master even though it's Monotonic by now.
result := M{}
cmd := session.DB("admin").C("$cmd")
err = cmd.Find(M{"ismaster": 1}).One(&result)
c.Assert(err, IsNil)
c.Assert(result["ismaster"], Equals, true)
}
示例11: TestAuthLoginChangePassword
func (s *S) TestAuthLoginChangePassword(c *C) {
session, err := mgo.Dial("localhost:40002")
c.Assert(err, IsNil)
defer session.Close()
admindb := session.DB("admin")
err = admindb.Login("root", "rapadura")
c.Assert(err, IsNil)
mydb := session.DB("mydb")
err = mydb.AddUser("myuser", "myoldpass", false)
c.Assert(err, IsNil)
err = mydb.Login("myuser", "myoldpass")
c.Assert(err, IsNil)
err = mydb.AddUser("myuser", "mynewpass", true)
c.Assert(err, IsNil)
err = mydb.Login("myuser", "mynewpass")
c.Assert(err, IsNil)
admindb.Logout()
// The second login must be in effect, which means read-only.
err = mydb.C("mycoll").Insert(M{"n": 1})
c.Assert(err, ErrorMatches, "unauthorized")
}
示例12: BuildMongoDatabase
func (c Config) BuildMongoDatabase() *mgo.Database {
sess, err := mgo.Dial(c.URL)
if err != nil {
panic(err)
}
return sess.DB(c.Name)
}
示例13: TestDirect
func (s *S) TestDirect(c *C) {
session, err := mgo.Dial("localhost:40012?connect=direct")
c.Assert(err, IsNil)
defer session.Close()
// We know that server is a slave.
session.SetMode(mgo.Monotonic, true)
result := &struct{ Host string }{}
err = session.Run("serverStatus", result)
c.Assert(err, IsNil)
c.Assert(strings.HasSuffix(result.Host, ":40012"), Equals, true)
stats := mgo.GetStats()
c.Assert(stats.SocketsAlive, Equals, 1)
c.Assert(stats.SocketsInUse, Equals, 1)
c.Assert(stats.SocketRefs, Equals, 1)
// We've got no master, so it'll timeout.
session.SetSyncTimeout(5e8 * time.Nanosecond)
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"test": 1})
c.Assert(err, ErrorMatches, "no reachable servers")
// Slave is still reachable.
result.Host = ""
err = session.Run("serverStatus", result)
c.Assert(err, IsNil)
c.Assert(strings.HasSuffix(result.Host, ":40012"), Equals, true)
}
示例14: main
func main() {
var err error
log.Println("Starting SMS demon.")
log.Println("dialing mongo db ...")
mgoSession, err = mgo.Dial("127.0.0.1")
if err != nil {
log.Fatal(err.Error())
return
}
defer mgoSession.Close()
log.Println("Ok, ready to go!")
ticker := time.NewTicker(minutes(1))
L:
for {
select {
case msg := <-control_chan:
if msg == "quit" {
break L
}
case <-ticker.C:
go DoJob()
}
}
}
示例15: TestTopologySyncWithSlaveSeed
func (s *S) TestTopologySyncWithSlaveSeed(c *C) {
// That's supposed to be a slave. Must run discovery
// and find out master to insert successfully.
session, err := mgo.Dial("localhost:40012")
c.Assert(err, IsNil)
defer session.Close()
coll := session.DB("mydb").C("mycoll")
coll.Insert(M{"a": 1, "b": 2})
result := struct{ Ok bool }{}
err = session.Run("getLastError", &result)
c.Assert(err, IsNil)
c.Assert(result.Ok, Equals, true)
// One connection to each during discovery. Master
// socket recycled for insert.
stats := mgo.GetStats()
c.Assert(stats.MasterConns, Equals, 1)
c.Assert(stats.SlaveConns, Equals, 2)
// Only one socket reference alive, in the master socket owned
// by the above session.
c.Assert(stats.SocketsInUse, Equals, 1)
// Refresh it, and it must be gone.
session.Refresh()
stats = mgo.GetStats()
c.Assert(stats.SocketsInUse, Equals, 0)
}