{"record":{"id":"8d3a3c8df27db50c","repo":"apache/beam","slug":"writetomongodb-coll-param-must-be-specified-as-a-string","errorCode":null,"errorMessage":"WriteToMongoDB coll param must be specified as a string","messagePattern":"WriteToMongoDB coll param must be specified as a string","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/mongodbio.py","lineNumber":709,"sourceCode":"      uri (str): The MongoDB connection string following the URI format\n      db (str): The MongoDB database name\n      coll (str): The MongoDB collection name\n      batch_size(int): Number of documents per bulk_write to  MongoDB,\n        default to 100\n      extra_client_params(dict): Optional `MongoClient\n       <https://api.mongodb.com/python/current/api/pymongo/mongo_client.html>`_\n       parameters as keyword arguments\n\n    Returns:\n      :class:`~apache_beam.transforms.ptransform.PTransform`\n\n    \"\"\"\n    if extra_client_params is None:\n      extra_client_params = {}\n    if not isinstance(db, str):\n      raise ValueError(\"WriteToMongoDB db param must be specified as a string\")\n    if not isinstance(coll, str):\n      raise ValueError(\n          \"WriteToMongoDB coll param must be specified as a string\")\n    self._uri = uri\n    self._db = db\n    self._coll = coll\n    self._batch_size = batch_size\n    self._spec = extra_client_params\n\n  def expand(self, pcoll):\n    return (\n        pcoll\n        | beam.ParDo(_GenerateObjectIdFn())\n        | Reshuffle()\n        | beam.ParDo(\n            _WriteMongoFn(\n                self._uri, self._db, self._coll, self._batch_size, self._spec)))\n\n\nclass _GenerateObjectIdFn(DoFn):","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/mongodbio.py#L691-L727","documentation":"WriteToMongoDB validates that both the `db` and `coll` constructor parameters are plain strings. If `coll` is passed as None or a non-string (int, bytes, etc.), __init__ raises this ValueError immediately at pipeline construction time. The sink needs both to build the MongoDB collection reference for writes.","triggerScenarios":"Calling WriteToMongoDB(uri, db, coll) where coll is None, an int, a bytes object, or any non-string value.","commonSituations":"Passing the collection name from a config/CLI arg that was never parsed to str (e.g. int), forgetting the coll argument so it defaults to None, or reading collection names from JSON/env vars as non-strings.","solutions":["Pass coll as a string, e.g. WriteToMongoDB(uri, db='mydb', coll='mycollection').","Coerce programmatically: coll=str(coll) before constructing the sink.","Validate inputs (db and coll both non-None str) before building the pipeline.","If coll comes from config, add a required-field check with type assertion at startup."],"exampleFix":"// before\nWriteToMongoDB('mongodb://localhost:27017', db='test', coll=None)\n// after\nWriteToMongoDB('mongodb://localhost:27017', db='test', coll='mycollection')","handlingStrategy":"validation","validationCode":"if not isinstance(coll, str):\n    raise ValueError(f\"coll must be a string, got {type(coll).__name__}\")","typeGuard":"def is_str(x) -> bool:\n    return isinstance(x, str)","tryCatchPattern":"try:\n    sink = WriteToMongoDB(uri, db=db, coll=coll)\nexcept ValueError as e:\n    logger.error(\"Invalid WriteToMongoDB config: %s\", e)\n    raise","preventionTips":["Always pass db and coll as explicit string literals or coerced str(config_value).","Validate pipeline sink args at config-parse time, not at pipeline runtime.","Use keyword arguments so a missing coll fails loudly."],"tags":["python","apache-beam","io","mongodb","type-error"],"backgroundTag":"type-mismatch","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"}