func NewFollowersDatabase() *FollowersDatabase { conn, err := mongo.Dial("127.0.0.1:27017") if err != nil { log.Println("mongo Connect error:", err.Error()) panic("mongo conn err") } if verboseMongo { conn = mongo.NewLoggingConn(conn, log.New(os.Stderr, "", 0), "") } db := mongo.Database{conn, DbName, mongo.DefaultLastErrorCmd} return &FollowersDatabase{ userFollowers: db.C(USER_FOLLOWERS_TABLE), userFollowersCounter: db.C(USER_FOLLOWERS_COUNTERS_TABLE), followPending: db.C(FOLLOW_PENDING_TABLE), previousUnfollows: db.C(PREVIOUS_UNFOLLOWS_TABLE), } }
// A function that Dials and Authenticates // into a provided data base // accespts host,db,user,pass,log=Bool // returns db,err func ConnAndAuth(h, d, u, p string, l bool) mongo.Database { conn, err := mongo.Dial(h) if err != nil { log.Fatal(err) } defer conn.Close() if l { conn = mongo.NewLoggingConn(conn, log.New(os.Stdout, "", 0), "") // clear log prefix for more readable output log.SetFlags(0) } db := mongo.Database{conn, d, mongo.DefaultLastErrorCmd} err = db.Authenticate(u, p) if err != nil { log.Fatal(err) } return db }
func main() { // Connect to server. conn, err := mongo.Dial("localhost") if err != nil { log.Fatal(err) } defer conn.Close() // Wrap connection with logger so that we can view the traffic to and from // the server. conn = mongo.NewLoggingConn(conn, log.New(os.Stdout, "", 0), "") // Clear the log prefix for more readable output. log.SetFlags(0) reset(conn) chapter1(conn) chapter2(conn) chapter3(conn) chapter7(conn) }