// Connect create connection and ping db
Connect create connection and ping db
// Connect create connection and ping db
func (m *Mongo) Connect(dbConnectionString string, maxConnectionsInPool int) error {
	var err error
	opts := options.Client()
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	m.db, err = mongo.Connect(ctx, opts.ApplyURI(dbConnectionString))
	if err != nil {
		return err
	}
	ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()
	err = m.db.Ping(ctx, readpref.Primary())
	if err != nil {
		return err
	}
	m.database = m.db.Database("hello_world")
	m.worlds = m.database.Collection("world")
	m.fortunes = m.database.Collection("fortune")
	return nil
}
func ( m * Mongo ) Connect ( dbConnectionString string , maxConnectionsInPool int ) error { var err error opts := options . Client ( ) ctx , cancel := context . WithTimeout ( context . Background ( ) , 60 * time . Second ) defer cancel ( ) m . db , err = mongo . Connect ( ctx , opts . ApplyURI ( dbConnectionString ) ) if err != nil { return err } ctx , cancel = context . WithTimeout ( context . Background ( ) , 2 * time . Second ) defer cancel ( ) err = m . db . Ping ( ctx , readpref . Primary ( ) ) if err != nil { return err } m . database = m . db . Database ( " " ) m . worlds = m . database . Collection ( " " ) m . fortunes = m . database . Collection ( " " ) return nil }
Connect create connection and ping db
Connect create connection and ping db
// opts.SetMaxPoolSize(uint16(maxConnectionsInPool))