Connection is not open when use mongo.Connect instead it does when I do the query

Solution 1:

mongo.Connect() creates a new mongo.Client and initializes it, but does not (necessarily) create connections to the DB server.

To actually create a connection and check if the server is reachable (without executing a query), you may use the Client.Ping() method. This will return an error if the server is not reachable.

The official mongodb driver manages an internal pool of connections. Connections are not closed immediately after use, instead they are put back into the pool so when a connection is needed to carry out an operation, an idle connection can be used from the pool immediately. This is the intended behavior. You may configure its size via the options.ClientOptions you pass to mongo.Connect().

See ClientOptions.SetMaxPoolSize():

SetMaxPoolSize specifies that maximum number of connections allowed in the driver's connection pool to each server. Requests to a server will block if this maximum is reached. This can also be set through the "maxPoolSize" URI option (e.g. "maxPoolSize=100"). The default is 100. If this is 0, it will be set to math.MaxInt64.

Example setting up a client with limited connections, and pinging it:

ctx := context.Background()

opts := options.Client().
    ApplyURI("mongodb://localhost").
    SetMaxPoolSize(20) // Allow no more than 20 connections per server

client, err := mongo.Connect(ctx, opts)
if err != nil {
    log.Printf("mongo.Connect() failed: %v", err)
    return
}
defer client.Disconnect(ctx)

if err := client.Ping(ctx, nil); err != nil {
    log.Printf("Can't connect to db: %v", err)
    return
}

// Use client

See related: goroutine create multiple mongodb connection