{"record":{"id":"48058aaed6f1eaec","repo":"apache/beam","slug":"mongodb-location-must-be-in-the-following-format-mongodb","errorCode":null,"errorMessage":"MongoDb location must be in the following format: 'mongodb://(username:password@)?localhost:27017/database/collection' but was: ${location}","messagePattern":"MongoDb location must be in the following format: 'mongodb://\\(username:password@\\)\\?localhost:27017/database/collection' but was: (.+?)","errorType":"validation","errorClass":"InvalidTableException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/mongodb/MongoDbTable.java","lineNumber":100,"sourceCode":"\n  // Should match: mongodb://username:password@localhost:27017/database/collection\n  @VisibleForTesting\n  final Pattern locationPattern =\n      Pattern.compile(\n          \"(?<credsHostPort>mongodb://(?<usernamePassword>.*(?<password>:.*)?@)?.+:\\\\d+)/(?<database>.+)/(?<collection>.+)\");\n\n  @VisibleForTesting final String dbCollection;\n  @VisibleForTesting final String dbName;\n  @VisibleForTesting final String dbUri;\n\n  MongoDbTable(Table table) {\n    super(table.getSchema());\n\n    String location = table.getLocation();\n    Matcher matcher = locationPattern.matcher(location);\n\n    if (!matcher.matches()) {\n      throw new InvalidTableException(\n          \"MongoDb location must be in the following format:\"\n              + \" 'mongodb://(username:password@)?localhost:27017/database/collection'\"\n              + \" but was: \"\n              + location);\n    }\n    this.dbUri = matcher.group(\"credsHostPort\"); // \"mongodb://localhost:27017\"\n    this.dbName = matcher.group(\"database\");\n    this.dbCollection = matcher.group(\"collection\");\n  }\n\n  @Override\n  public PCollection<Row> buildIOReader(PBegin begin) {\n    // Read MongoDb Documents\n    PCollection<Document> readDocuments =\n        MongoDbIO.read()\n            .withUri(dbUri)\n            .withDatabase(dbName)\n            .withCollection(dbCollection)","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/mongodb/MongoDbTable.java#L82-L118","documentation":"MongoDbTable's constructor validates the table 'location' string against a regex requiring the form mongodb://[user:pass@]host:port/database/collection. If the location does not match, InvalidTableException is thrown during table resolution, before any connection attempt.","triggerScenarios":"Creating a MongoDb external table whose LOCATION is missing 'mongodb://', lacks a database/collection path segment, uses an unsupported scheme (mongodb+srv://), or contains characters the regex rejects.","commonSituations":"Pasting a MongoDB Atlas SRV connection string (mongodb+srv://) which the legacy regex does not accept; omitting the database or collection from the URI; quoting/whitespace issues in the DDL.","solutions":["Rewrite LOCATION as 'mongodb://host:port/database/collection', including both database and collection.","For SRV URIs, resolve the actual host list and use a plain mongodb:// host:port URI the regex accepts.","Escape or remove credentials issues by either including user:password@ fully or omitting credentials entirely (not partially)."],"exampleFix":"// before\nCREATE EXTERNAL TABLE t (...) LOCATION 'mongodb+srv://cluster.example.net/db.coll'\n// after\nCREATE EXTERNAL TABLE t (...) LOCATION 'mongodb://user:pass@host1:27017/db.coll'","handlingStrategy":"validation","validationCode":"Pattern p = Pattern.compile(\"^mongodb://([a-zA-Z0-9_.:-]+@)?[a-zA-Z0-9_.:-]+:[0-9]+/[a-zA-Z0-9_]+/[a-zA-Z0-9_]+$\");\nif (!p.matcher(location).matches()) throw new IllegalArgumentException(\"Bad MongoDb LOCATION: \" + location);","typeGuard":null,"tryCatchPattern":"try { new MongoDbTable(table); } catch (InvalidTableException e) { /* surface a formatted-location hint to the user */ }","preventionTips":["Always include scheme, host:port, database, and collection","Avoid mongodb+srv:// in Beam SQL locations","Template the LOCATION string from config instead of hand-writing it"],"tags":["java","apache-beam","mongodb","sql","url"],"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-14T16:17:12.679Z"}