{"record":{"id":"e22899443b6094ab","repo":"apache/beam","slug":"error-connecting-to-mongodb-w","errorCode":null,"errorMessage":"error connecting to MongoDB: %w","messagePattern":"error connecting to MongoDB: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"sdks/go/pkg/beam/io/mongodbio/common.go","lineNumber":61,"sourceCode":"\t\tclient, err := newClient(ctx, fn.URI)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\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 {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/common.go#L43-L79","documentation":"Returned by newClient in mongodbio when mongo.Connect fails to establish a client from the given URI. Setup calls this during pipeline start, so the whole source/sink fails immediately. It wraps the underlying driver error.","triggerScenarios":"Calling Setup (via newClient) with a malformed MongoDB connection URI, unknown scheme, or invalid options (e.g. bad replicaSet parameter).","commonSituations":"Typo'd URI in pipeline options; URL-special characters in password not percent-encoded; wrong scheme (mongodb vs mongodb+srv); driver version mismatch after upgrade.","solutions":["Validate the connection URI format: mongodb://[user:pass@]host[:port]/[db][?options] or mongodb+srv://.","Percent-encode special characters in username/password (e.g. @ -> %40).","Confirm the driver version supports the URI options used (e.g. +srv requires driver >= 1.x with DNS support).","Test the same URI with mongosh or a small Go snippet to isolate the invalid option."],"exampleFix":"// before: raw password with special chars\nuri := \"mongodb://user:p@ssw0rd@host:27017/db\"\n\n// after: percent-encoded credentials\nuri := \"mongodb://user:p%40ssw0rd@host:27017/db\"","handlingStrategy":"validation","validationCode":"func validMongoURI(uri string) error {\n    u, err := url.Parse(uri)\n    if err != nil { return err }\n    if u.Scheme != \"mongodb\" && u.Scheme != \"mongodb+srv\" { return fmt.Errorf(\"bad scheme %q\", u.Scheme) }\n    if u.Host == \"\" { return fmt.Errorf(\"missing host\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"client, err := newClient(ctx, uri)\nif err != nil {\n    return fmt.Errorf(\"mongo setup failed for %q: %w\", redactURI(uri), err)\n}","preventionTips":["Store the MongoDB URI in config/flags, validated at startup with url.Parse.","Percent-encode username and password (url.UserPassword handles this).","Use the correct scheme (mongodb+srv for Atlas SRV records).","Smoke-test the URI with mongosh before deploying the pipeline."],"tags":["mongodb","connection","network","configuration"],"backgroundTag":"invalid-url-format","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"}