apache/beam · error
mongodbio.newReadFn: %v
Error message
mongodbio.newReadFn: %v
What it means
newReadFn encodes the ReadOption's filter into BSON before building the read DoFn. If encodeBSON fails (e.g. the filter contains values not marshalable to BSON), it panics because the transform cannot be constructed. This happens at pipeline-construction time, not at runtime.
Source
Thrown at sdks/go/pkg/beam/io/mongodbio/read.go:115
mongoDBFn
BucketAuto bool
BundleSize int64
Filter []byte
Type beam.EncodedType
filter bson.M
projection bson.D
}
func newReadFn(
uri string,
database string,
collection string,
t reflect.Type,
option *ReadOption,
) *readFn {
filter, err := encodeBSON[bson.M](option.Filter)
if err != nil {
panic(fmt.Sprintf("mongodbio.newReadFn: %v", err))
}
return &readFn{
mongoDBFn: mongoDBFn{
URI: uri,
Database: database,
Collection: collection,
},
BucketAuto: option.BucketAuto,
BundleSize: option.BundleSize,
Filter: filter,
Type: beam.EncodedType{T: t},
}
}
func (fn *readFn) Setup(ctx context.Context) error {
var err error
if err = fn.mongoDBFn.Setup(ctx); err != nil {View on GitHub (pinned to 12126d8942)
Solutions
- Ensure option.Filter is a bson.M / map[string]interface{} with BSON-supported value types
- Test bson.Marshal(filter) standalone before building the pipeline to reproduce the marshal error
- Replace unsupported field types (e.g. time in wrong format, non-UTF8 strings, funcs)
- Upgrade the mongo-driver if the marshal error is a known type-support issue
Example fix
// before
opt.Filter = map[string]interface{}{"ts": someChannelOrFunc}
mongodbio.Read(s, scope, uri, db, col, typ, opt)
// after
opt.Filter = bson.M{"ts": bson.M{"$gte": time.Now().Add(-time.Hour)}}
mongodbio.Read(s, scope, uri, db, col, typ, opt) Defensive patterns
Strategy: validation
Validate before calling
if _, err := bson.Marshal(option.Filter); err != nil {
return fmt.Errorf("filter not BSON-marshalable: %w", err)
} Type guard
func isBSONSafe(m bson.M) bool { b, err := bson.Marshal(m); return err == nil && b != nil } Try / catch
defer func() { if r := recover(); r != nil { log.Fatalf("readFn construction: %v", r) } }() Prevention
- Build filters only from BSON-supported types (bson.M, primitives, time.Time)
- Dry-run bson.Marshal in tests for all filter shapes
- Avoid embedding unmarshalable types like funcs or channels in filters
When it happens
Trigger: Calling Read with a Filter option containing unsupported Go types (channels, funcs, cyclic structures) or an invalid shape that mongo's bson marshaler rejects.
Common situations: Building filters dynamically with unmarshaled JSON producing types bson cannot encode; embedding a struct with no BSON-marshalable fields; driver version incompatibilities with map types.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- error encoding BSON: %w
- error decoding BSON: %w
- error decoding document ID: %w
- error decoding document: %w
- err
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/1829e66a1db702d2.
Report an issue: GitHub.