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 }
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) }
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) }
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()) }
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:pass@]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 }
// 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 }
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) }
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") }
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) }
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) }
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") }
func (c Config) BuildMongoDatabase() *mgo.Database { sess, err := mgo.Dial(c.URL) if err != nil { panic(err) } return sess.DB(c.Name) }
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) }
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() } } }
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) }
func (s *S) TestGridFSOpen(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() file, err = gfs.Open("myfile.txt") c.Assert(err, IsNil) defer file.Close() var b [1]byte _, err = file.Read(b[:]) c.Assert(err, IsNil) c.Assert(string(b[:]), Equals, "2") }
func (s *S) TestAuthAddUser(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("myruser", "mypass", true) c.Assert(err, IsNil) err = mydb.AddUser("mywuser", "mypass", false) c.Assert(err, IsNil) err = mydb.Login("myruser", "mypass") c.Assert(err, IsNil) admindb.Logout() coll := session.DB("mydb").C("mycoll") err = coll.Insert(M{"n": 1}) c.Assert(err, ErrorMatches, "unauthorized") err = mydb.Login("mywuser", "mypass") c.Assert(err, IsNil) err = coll.Insert(M{"n": 1}) c.Assert(err, IsNil) }
func (s *S) UpcomingTestRemovalOfClusterMember(c *C) { session, err := mgo.Dial("localhost:40011") // Always the master for rs1. c.Assert(err, IsNil) defer func() { session.Refresh() session.Run(bson.D{{"$eval", `rs.add("127.0.0.1:40012")`}}, nil) session.Close() // XXX It's losing the priority setting here. Must use rs2 and detect master. }() err = session.Run(bson.D{{"$eval", `rs.remove("127.0.0.1:40012")`}}, nil) c.Assert(err, Equals, io.EOF) session.Refresh() for i := 0; i < 15; i++ { if len(session.LiveServers()) == 2 { break } time.Sleep(time.Second) } live := session.LiveServers() if len(live) != 2 { c.Errorf("Removed server still considered live: %#s", live) } }
func ExampleAdmin() { session, err := mgo.Dial("mongo://my_mongo_server") if err != nil { log.Fatal(err) } a := &Admin{ Session: session, Renderer: &TestRenderer{}, Routes: map[string]string{ "index": "/1/", "list": "/2/", "update": "/3/", "create": "/4/", "detail": "/5/", "delete": "/6/", "auth": "/7/", }, Prefix: "/admin", } a.Register(T{}, "database.collection", &Options{ Columns: []string{"First", "Second", "Fifth"}, }) http.Handle("/admin/", a) if err := http.ListenAndServe(":11223", nil); err != nil { log.Fatal(err) } }
// Tests tokenization func BenchmarkRoundTrip(b *testing.B) { b.StopTimer() log.SetFlags(log.Ltime | log.Lshortfile) session, err := mgo.Dial("localhost") if err != nil { b.Fatal("Could not connect to MongoDB:", err) } db := session.DB("test_gokenizer_tokenizer") err = db.DropDatabase() if err != nil { b.Fatal("Could not drop test db:", err) } tokenizer := NewMongoTokenizer(db) b.StartTimer() for i := 0; i < b.N; i++ { orig := goutil.RandAlphanumeric(8, 8) token, err := tokenizer.Tokenize(orig) if err != nil { b.Error("Tokenize error:", err) } var detok string // Result of detokenization - should be same as orig detok, err = tokenizer.Detokenize(token) if err != nil { b.Error("Detokenize error:", err) } if detok != orig { msg := "Detokenization failed: '%s' != '%s'." msg = fmt.Sprintf(msg, orig, detok) b.Error(msg) } } }
func (s *S) TestGridFSReadChunking(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("") c.Assert(err, IsNil) id := file.Id() file.SetChunkSize(5) n, err := file.Write([]byte("abcdefghijklmnopqrstuv")) c.Assert(err, IsNil) c.Assert(n, Equals, 22) err = file.Close() c.Assert(err, IsNil) file, err = gfs.OpenId(id) c.Assert(err, IsNil) b := make([]byte, 30) // Smaller than the chunk size. n, err = file.Read(b[:3]) c.Assert(err, IsNil) c.Assert(n, Equals, 3) c.Assert(b[:3], Equals, []byte("abc")) // Boundary in the middle. n, err = file.Read(b[:4]) c.Assert(err, IsNil) c.Assert(n, Equals, 4) c.Assert(b[:4], Equals, []byte("defg")) // Boundary at the end. n, err = file.Read(b[:3]) c.Assert(err, IsNil) c.Assert(n, Equals, 3) c.Assert(b[:3], Equals, []byte("hij")) // Larger than the chunk size, with 3 chunks. n, err = file.Read(b) c.Assert(err, IsNil) c.Assert(n, Equals, 12) c.Assert(b[:12], Equals, []byte("klmnopqrstuv")) n, err = file.Read(b) c.Assert(n, Equals, 0) c.Assert(err == io.EOF, Equals, true) err = file.Close() c.Assert(err, IsNil) }
func Run(logger *log.Logger, dbserver, dbname string) (err error) { session, err := mgo.Dial(os.Args[1]) if err != nil { logger.Printf("error: can't connect to mongodb server @ %s", os.Args[1]) return err } b := newrbot(session, os.Args[2], logger) freq, err := strconv.ParseInt(b.config["frequency"], 10, 0) if err != nil { logger.Printf("error: can't parse frequency from the config db\n") return (err) } if freq < 1 { freq = 60 b.logger.Printf("using a default value of %d seconds for the frequency\n", freq) } else { b.logger.Printf("using a value of %d seconds for the frequency\n", freq) } for { feed, err := b.FetchAtomFeed() if err == nil { newarticlecount, err := b.StoreNewEntries(feed.Entries) if err != nil { b.logger.Printf("failed to StoreNewEntries (%s)\n", err) return err } postedcount, queuedcount, err := b.PostOneNewArticle() if err != nil { b.logger.Printf("failed to PostOneNewArticle (%s)\n", err) return err } b.logger.Printf("new: %d, posted: %d, queued: %d\n", newarticlecount, postedcount, queuedcount) } else { b.logger.Printf("error fetching atom feed (%s)\n", err) } time.Sleep(time.Duration(freq) * time.Second) } return nil }
func (s *S) TestAuthURLWrongCredentials(c *C) { session, err := mgo.Dial("mongodb://*****:*****@localhost:40002/") if session != nil { session.Close() } c.Assert(err, ErrorMatches, "auth fails") c.Assert(session, IsNil) }
func CreateMongoDB(host string, dbname string) (*MongoDB, error) { session, err := mgo.Dial(host) if err != nil { return nil, err } m := &MongoDB{Session: session, DBName: dbname} return m, nil }
func (s *S) TestAuthURL(c *C) { session, err := mgo.Dial("mongodb://*****:*****@localhost:40002/") c.Assert(err, IsNil) defer session.Close() err = session.DB("mydb").C("mycoll").Insert(M{"n": 1}) c.Assert(err, IsNil) }
func TestMongoFlashes(t *testing.T) { s, err := mgo.Dial("localhost") if err != nil { t.Fatal(err) } store := NewMongoStore(s.DB("db").C("collection"), []byte("secret-key")) testSessionFlashes(t, store) }
func (s *S) TestGridFSOpenNext(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("myfile1.txt") c.Assert(err, IsNil) file.Write([]byte{'1'}) file.Close() file, err = gfs.Create("myfile2.txt") c.Assert(err, IsNil) file.Write([]byte{'2'}) file.Close() var f *mgo.GridFile var b [1]byte iter := gfs.Find(nil).Sort(bson.M{"filename": -1}).Iter() ok := gfs.OpenNext(iter, &f) c.Assert(ok, Equals, true) c.Check(f.Name(), Equals, "myfile2.txt") _, err = f.Read(b[:]) c.Assert(err, IsNil) c.Assert(string(b[:]), Equals, "2") ok = gfs.OpenNext(iter, &f) c.Assert(ok, Equals, true) c.Check(f.Name(), Equals, "myfile1.txt") _, err = f.Read(b[:]) c.Assert(err, IsNil) c.Assert(string(b[:]), Equals, "1") ok = gfs.OpenNext(iter, &f) c.Assert(ok, Equals, false) c.Assert(iter.Err(), Equals, nil) c.Assert(f, IsNil) // Do it again with a more restrictive query to make sure // it's actually taken into account. iter = gfs.Find(bson.M{"filename": "myfile1.txt"}).Iter() ok = gfs.OpenNext(iter, &f) c.Assert(ok, Equals, true) c.Check(f.Name(), Equals, "myfile1.txt") ok = gfs.OpenNext(iter, &f) c.Assert(ok, Equals, false) c.Assert(iter.Err(), Equals, nil) c.Assert(f, IsNil) }
func openMgoSession(t *testing.T) *mgo.Session { mgo.SetLogger(log.New(os.Stderr, "MONGO ", log.LstdFlags)) mgo.SetDebug(true) s, err := mgo.Dial("localhost") if err != nil { t.Fatalf("Error opening mongo session. %v", err) } return s }
func initDB() { session, err := mgo.Dial("localhost") if err != nil { log.Panicln("NisePostGo: ", err) os.Exit(1) } session.SetMode(mgo.Monotonic, true) db = session.DB("NisePostGo") }
func getOpCounters(server string) (c *OpCounters, err error) { session, err := mgo.Dial(server + "?connect=direct") if err != nil { return nil, err } defer session.Close() session.SetMode(mgo.Monotonic, true) result := struct{ OpCounters }{} err = session.Run("serverStatus", &result) return &result.OpCounters, err }