apache/beam · error · ValueError

WriteToMongoDB coll param must be specified as a string

Error message

WriteToMongoDB coll param must be specified as a string

What it means

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.

Solutions

  1. Pass coll as a string, e.g. WriteToMongoDB(uri, db='mydb', coll='mycollection').
  2. Coerce programmatically: coll=str(coll) before constructing the sink.
  3. Validate inputs (db and coll both non-None str) before building the pipeline.
  4. If coll comes from config, add a required-field check with type assertion at startup.

Example fix

// before
WriteToMongoDB('mongodb://localhost:27017', db='test', coll=None)
// after
WriteToMongoDB('mongodb://localhost:27017', db='test', coll='mycollection')
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(coll, str):
    raise ValueError(f"coll must be a string, got {type(coll).__name__}")

Type guard

def is_str(x) -> bool:
    return isinstance(x, str)

Try / catch

try:
    sink = WriteToMongoDB(uri, db=db, coll=coll)
except ValueError as e:
    logger.error("Invalid WriteToMongoDB config: %s", e)
    raise

Prevention

When it happens

Trigger: Calling WriteToMongoDB(uri, db, coll) where coll is None, an int, a bytes object, or any non-string value.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/8d3a3c8df27db50c. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/io/mongodbio.py:709

      uri (str): The MongoDB connection string following the URI format
      db (str): The MongoDB database name
      coll (str): The MongoDB collection name
      batch_size(int): Number of documents per bulk_write to  MongoDB,
        default to 100
      extra_client_params(dict): Optional `MongoClient
       <https://api.mongodb.com/python/current/api/pymongo/mongo_client.html>`_
       parameters as keyword arguments

    Returns:
      :class:`~apache_beam.transforms.ptransform.PTransform`

    """
    if extra_client_params is None:
      extra_client_params = {}
    if not isinstance(db, str):
      raise ValueError("WriteToMongoDB db param must be specified as a string")
    if not isinstance(coll, str):
      raise ValueError(
          "WriteToMongoDB coll param must be specified as a string")
    self._uri = uri
    self._db = db
    self._coll = coll
    self._batch_size = batch_size
    self._spec = extra_client_params

  def expand(self, pcoll):
    return (
        pcoll
        | beam.ParDo(_GenerateObjectIdFn())
        | Reshuffle()
        | beam.ParDo(
            _WriteMongoFn(
                self._uri, self._db, self._coll, self._batch_size, self._spec)))


class _GenerateObjectIdFn(DoFn):

View on GitHub (pinned to 12126d8942)