{"record":{"id":"41dbd92984d7debd","repo":"apache/beam","slug":"readfrommongdb-db-param-must-be-specified-as-a-string","errorCode":null,"errorMessage":"ReadFromMongDB db param must be specified as a string","messagePattern":"ReadFromMongDB 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":153,"sourceCode":"        specifying elements which must be present for a document to be included\n        in the result set.\n      projection: A list of field names that should be returned in the result\n        set or a dict specifying the fields to include or exclude.\n      extra_client_params(dict): Optional `MongoClient\n        <https://api.mongodb.com/python/current/api/pymongo/mongo_client.html>`_\n        parameters.\n      bucket_auto (bool): If :data:`True`, use MongoDB `$bucketAuto` aggregation\n        to split collection into bundles instead of `splitVector` command,\n        which does not work with MongoDB Atlas.\n        If :data:`False` (the default), use `splitVector` command for bundling.\n\n    Returns:\n      :class:`~apache_beam.transforms.ptransform.PTransform`\n    \"\"\"\n    if extra_client_params is None:\n      extra_client_params = {}\n    if not isinstance(db, str):\n      raise ValueError(\"ReadFromMongDB db param must be specified as a string\")\n    if not isinstance(coll, str):\n      raise ValueError(\n          \"ReadFromMongDB coll param must be specified as a string\")\n    self._mongo_source = _BoundedMongoSource(\n        uri=uri,\n        db=db,\n        coll=coll,\n        filter=filter,\n        projection=projection,\n        extra_client_params=extra_client_params,\n        bucket_auto=bucket_auto,\n    )\n\n  def expand(self, pcoll):\n    return pcoll | iobase.Read(self._mongo_source)\n\n\nclass _ObjectIdRangeTracker(OrderedPositionRangeTracker):","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/mongodbio.py#L135-L171","documentation":"ReadFromMongoDB.__init__ validates that the db parameter is a Python str and raises ValueError otherwise. The database name is passed straight into the MongoDB URI/client config, so non-string values (bytes, None, ObjectId) would produce invalid client parameters downstream.","triggerScenarios":"Calling ReadFromMongoDB(uri=..., db=<non-string>, coll=...) e.g. db=None because a config variable was unset, or db read as bytes from an environment/config file.","commonSituations":"Loading db name from env vars/config that returned None; passing bytes from os.environb or YAML-parsed values; typos passing a positional arg into db.","solutions":["Pass the db name as a str, e.g. db='mydb'.","If sourced from config/env, coerce with str(value) and check it is not None/empty first.","Decode bytes with .decode('utf-8') before passing.","Fix argument order so a different value isn't landing in db."],"exampleFix":"// before\np = ReadFromMongoDB(uri=uri, db=os.environ.get('MONGO_DB'), coll='users')\n// after\ndb = os.environ.get('MONGO_DB')\nassert isinstance(db, str) and db\np = ReadFromMongoDB(uri=uri, db=db, coll='users')","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    t = ReadFromMongoDB(uri=uri, db=db, coll=coll)\nexcept ValueError as e:\n    raise ConfigError(f'bad Mongo source config: {e}') from e","preventionTips":["Validate db/coll are non-empty strings at config load time.","Decode bytes config values explicitly.","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"}