{"record":{"id":"746c7a4ed8692a2c","repo":"apache/beam","slug":"failed-to-convert-bson-document-to-beam-row-doc-tojson","errorCode":null,"errorMessage":"Failed to convert BSON Document to Beam Row: \" + doc.toJson()","messagePattern":"Failed to convert BSON Document to Beam Row: \" \\+ doc\\.toJson\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbReadSchemaTransformProvider.java","lineNumber":137,"sourceCode":"  /** Converts a MongoDB BSON {@link Document} to a Beam {@link Row}. */\n  static class DocumentToRowFn extends DoFn<Document, Row> {\n    private final Schema schema;\n    private final boolean handleErrors;\n    private final Schema errorSchema;\n\n    DocumentToRowFn(Schema schema, boolean handleErrors, Schema errorSchema) {\n      this.schema = schema;\n      this.handleErrors = handleErrors;\n      this.errorSchema = errorSchema;\n    }\n\n    @ProcessElement\n    public void processElement(@Element Document doc, MultiOutputReceiver receiver) {\n      try {\n        receiver.get(OUTPUT_TAG).output(MongoDbUtils.toRow(doc, schema));\n      } catch (Exception e) {\n        if (!handleErrors) {\n          throw new RuntimeException(\n              \"Failed to convert BSON Document to Beam Row: \" + doc.toJson(), e);\n        }\n        errorCounter.inc();\n        byte[] docBytes;\n        try {\n          docBytes = doc.toJson().getBytes(java.nio.charset.StandardCharsets.UTF_8);\n        } catch (Exception jsonEx) {\n          docBytes = doc.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);\n        }\n        receiver.get(ERROR_TAG).output(ErrorHandling.errorRecord(errorSchema, docBytes, e));\n      }\n    }\n  }\n}\n","sourceCodeStart":119,"sourceCodeEnd":152,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbReadSchemaTransformProvider.java#L119-L152","documentation":"In MongoDbReadSchemaTransformProvider's read DoFn, each BSON Document is converted to a Beam Row via MongoDbUtils.toRow. If conversion throws and error handling (handleErrors) is disabled, the DoFn wraps the failure in a RuntimeException that includes the document's JSON for debugging.","triggerScenarios":"A Document from MongoDB whose BSON shape does not match the configured schema (e.g., mismatched field types, unsupported BSON types) passed to toRow while handleErrors is false.","commonSituations":"MongoDB collections with heterogeneous documents or fields that changed type over time; schema configured in the transform that doesn't match live collection data.","solutions":["Fix the configured schema to match the actual BSON field types in the collection","Enable error handling (handleErrors true) so bad documents are routed to the error output instead of failing the pipeline","Pre-clean/transform the collection data or filter incompatible documents before the read transform"],"exampleFix":"// before\nMongoDb.read(schemaConfig).withHandleErrors(false);\n// after\nMongoDb.read(schemaConfig).withHandleErrors(true); // bad docs go to error output\n// or fix schema so doc types match:\n// schema field \"age\" INT64 but docs store STRING -> change schema to STRING or migrate data","handlingStrategy":"validation","validationCode":"// before running, sample documents and validate against schema\nfor (Document doc : sampleDocs) {\n  try {\n    MongoDbUtils.toRow(doc, schema);\n  } catch (Exception e) {\n    throw new IllegalStateException(\"Schema mismatch: \" + doc.toJson(), e);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  Row row = MongoDbUtils.toRow(doc, schema);\n} catch (RuntimeException e) {\n  if (e.getMessage().startsWith(\"Failed to convert BSON Document to Beam Row\")) {\n    // enable handleErrors or quarantine document by JSON\n  }\n}","preventionTips":["Enable handleErrors so bad documents flow to the error output instead of failing the pipeline","Sample and validate collection documents against the configured schema before deployment","Watch for BSON type drift in MongoDB collections (fields changing type over time)"],"tags":["java","apache-beam","mongodb","schema-mismatch","data-conversion"],"backgroundTag":"schema-validation-failed","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"}