{"record":{"id":"dd31fc7a160737a8","repo":"apache/beam","slug":"error-pinging-mongodb-w","errorCode":null,"errorMessage":"error pinging MongoDB: %w","messagePattern":"error pinging MongoDB: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"sdks/go/pkg/beam/io/mongodbio/common.go","lineNumber":65,"sourceCode":"\n\t\tfn.client = client\n\t}\n\n\tfn.collection = fn.client.Database(fn.Database).Collection(fn.Collection)\n\n\treturn nil\n}\n\nfunc newClient(ctx context.Context, uri string) (*mongo.Client, error) {\n\topts := options.Client().ApplyURI(uri)\n\n\tclient, err := mongo.Connect(ctx, opts)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error connecting to MongoDB: %w\", err)\n\t}\n\n\tif err := client.Ping(ctx, readpref.Primary()); err != nil {\n\t\treturn nil, fmt.Errorf(\"error pinging MongoDB: %w\", err)\n\t}\n\n\treturn client, nil\n}\n\nfunc (fn *mongoDBFn) Teardown(ctx context.Context) error {\n\tif err := fn.client.Disconnect(ctx); err != nil {\n\t\treturn fmt.Errorf(\"error disconnecting from MongoDB: %w\", err)\n\t}\n\n\treturn nil\n}\n\ntype documentID struct {\n\tID any `bson:\"_id\"`\n}\n\nfunc findID(","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/common.go#L47-L83","documentation":"Returned by newClient in mongodbio when client.Ping against the primary fails. The client was constructed, but no MongoDB server could actually be reached or authenticated, so Setup aborts. It wraps the underlying driver/ping error.","triggerScenarios":"Calling Setup where the MongoDB host is unreachable (wrong host/port, DNS failure for mongodb+srv), TLS handshake fails, or credentials are wrong (auth failure on ping).","commonSituations":"Firewall/security-group blocking port 27017; MongoDB not running or replica set not elected a primary; wrong authSource or credentials; VPC/network misconfig in containerized or Beam worker environments; TLS CA mismatch.","solutions":["Verify network reachability from the worker to host:port (test with `nc -vz host 27017` or ping).","Check that the MongoDB server/replica set is up and has a PRIMARY elected.","Validate credentials and authSource URI option; test the same URI with mongosh.","If using TLS/mongodb+srv, verify DNS SRV resolution and CA certificate configuration.","Retry on transient network failures; inspect the wrapped error (e.g. connection refused, auth failed) for the exact cause."],"exampleFix":"// before\nuri := \"mongodb://user:pass@internal-host:27017/db\" // authSource defaults to admin\n\n// after: explicit authSource and correct host\nuri := \"mongodb://user:pass@db.internal:27017/db?authSource=admin&connectTimeoutMS=5000\"","handlingStrategy":"retry","validationCode":"// reachability precheck before Setup:\nconn, err := net.DialTimeout(\"tcp\", hostPort, 5*time.Second)\nif err != nil { return fmt.Errorf(\"mongo unreachable at %s: %w\", hostPort, err) }\nconn.Close()","typeGuard":null,"tryCatchPattern":"client, err := newClient(ctx, uri)\nif err != nil {\n    if isNetworkError(err) { /* backoff + retry Setup */ }\n    if strings.Contains(err.Error(), \"auth\") { return ErrMongoAuth }\n    return err\n}","preventionTips":["Open firewall/security-group egress from Beam workers to MongoDB on 27017 (or 443/srv).","Ensure the replica set has a PRIMARY elected before starting pipelines.","Set connectTimeoutMS/serverSelectionTimeoutMS in the URI for fail-fast behavior.","Verify credentials and authSource against mongosh before deployment.","Add readiness checks that ping MongoDB before launching the job."],"tags":["mongodb","connection","network","authentication"],"backgroundTag":"connection-refused","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}