Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

I'm writing a Go app using mongo-driver to connect to the mongo replica set.

I've noticed that mongo.Connect is not actually connect to the database.

Even if I've shutdown the mongod instance, mongo.Connect sill able to pass through.

However, when I do the query it will connect to the mongod instance.

Now my problem is I have a lot (>100) concurrent queries to different databases within the same mongod instance.

The driver create a whole bunch of connections and mongod failed me Too many files opened because there is too many connections, Even I use a single mongo.Client.

Is this a proper behavior of mongo_driver and how can I deal with this?

Does MongoDB require each connection per each database?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda