{"record":{"id":"785acf85a56ea12d","repo":"apache/beam","slug":"writetomongodb-db-param-must-be-specified-as-a-string","errorCode":null,"errorMessage":"WriteToMongoDB db param must be specified as a string","messagePattern":"WriteToMongoDB db param must be specified as a string","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/mongodbio.py","lineNumber":707,"sourceCode":"\n    Args:\n      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","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/mongodbio.py#L689-L725","documentation":"WriteToMongoDB.__init__ validates that the db parameter is a Python str and raises ValueError otherwise. The database name is stored and later used to build the MongoDB client, so non-string values would form invalid write targets.","triggerScenarios":"Calling WriteToMongoDB(uri=..., db=<non-string>, coll=...) — db=None from an unset config, bytes from a config file, or a positional-argument mix-up.","commonSituations":"Unset environment variables for the sink database; YAML/JSON config returning non-str types; refactoring that swapped db/coll arguments.","solutions":["Pass the db name as a str, e.g. db='mydb'.","Coerce config/env values: check for None, then str(value) or bytes.decode('utf-8').","Add an upstream assertion that the db name is a non-empty string before constructing the transform.","Fix argument order so the intended value reaches db."],"exampleFix":"// before\nsink = WriteToMongoDB(uri=uri, db=config.get('db'), coll='results')\n// after\ndb = config.get('db')\nassert isinstance(db, str) and db\nsink = WriteToMongoDB(uri=uri, db=db, coll='results')","handlingStrategy":"type-guard","validationCode":"if not isinstance(db, str) or not db:\n    raise ValueError('db must be a non-empty string')","typeGuard":"def is_db_name(v) -> bool:\n    return isinstance(v, str) and bool(v)","tryCatchPattern":"try:\n    sink = WriteToMongoDB(uri=uri, db=db, coll=coll)\nexcept ValueError as e:\n    raise ConfigError(f'bad Mongo sink config: {e}') from e","preventionTips":["Validate db/coll are non-empty strings at config load time.","Handle unset env/config values before constructing the transform.","Use keyword arguments to avoid positional mix-ups."],"tags":["mongodb","validation","python"],"backgroundTag":"invalid-argument-value","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"}