{"record":{"id":"b839b0da7adad6a9","repo":"apache/beam","slug":"error-decoding-document-id-w","errorCode":null,"errorMessage":"error decoding document ID: %w","messagePattern":"error decoding document ID: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/read.go","lineNumber":312,"sourceCode":"\tctx context.Context,\n\tfilter bson.M,\n) (*mongo.Cursor, error) {\n\topts := options.Find().\n\t\tSetProjection(fn.projection).\n\t\tSetSort(bson.M{\"_id\": 1})\n\n\tcursor, err := fn.collection.Find(ctx, filter, opts)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error executing find command: %w\", err)\n\t}\n\n\treturn cursor, nil\n}\n\nfunc decodeDocument(cursor *mongo.Cursor, t reflect.Type) (id any, value any, err error) {\n\tvar docID documentID\n\tif err := cursor.Decode(&docID); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"error decoding document ID: %w\", err)\n\t}\n\n\tout := reflect.New(t).Interface()\n\tif err := cursor.Decode(out); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"error decoding document: %w\", err)\n\t}\n\n\tvalue = reflect.ValueOf(out).Elem().Interface()\n\n\treturn docID.ID, value, nil\n}\n","sourceCodeStart":294,"sourceCodeEnd":324,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/read.go#L294-L324","documentation":"decodeDocument first decodes each cursor document into a documentID struct to extract the _id used for tracking positions. If that decode fails, the error is wrapped as 'error decoding document ID'. This means a returned document's _id could not be represented by the driver's documentID type (unusual _id types or malformed documents).","triggerScenarios":"ProcessElement iterates the cursor and calls decodeDocument; cursor.Decode(&docID) fails — the document's _id uses a BSON type that cannot decode into the internal documentID struct (e.g. binary subtype, very exotic types), or the cursor/stream yields corrupt or interrupted data.","commonSituations":"Collections with heterogeneous or unusual _id types (e.g. arrays, regex, embedded null bytes); driver version that can't handle a server-inserted BSON type; network corruption or cursor invalidated mid-read; reading system/admin collections with odd documents.","solutions":["Inspect the failing document's _id type; migrate or normalize exotic _id values to supported types (string, ObjectId, int64)","Update the mongo-driver to the latest version for broader BSON type support","Exclude unsupported documents from the read range via the filter","Check driver/server version compatibility if a new BSON type appeared after a server upgrade","Wrap reads with retry logic for transient cursor errors"],"exampleFix":"// before — collection stores _id as mixed types including arrays\ncoll.InsertOne(ctx, bson.M{\"_id\": []int{1,2}, \"v\": 1})\n// after — use a stable scalar id\ncoll.InsertOne(ctx, bson.M{\"_id\": primitive.NewObjectID(), \"v\": 1})","handlingStrategy":"validation","validationCode":"// verify all _id types in the range are scalars the driver supports\ncur, _ := coll.Find(ctx, bson.M{\"_id\": bson.M{\"$type\": bson.A{\"array\",\"regex\",\"javascript\"}}})\nif n, _ := countLeftovers(cur, ctx); n > 0 { /* migrate or exclude them first */ }","typeGuard":null,"tryCatchPattern":"id, _, err := decodeDocument(cursor, t)\nif err != nil {\n\tlog.Printf(\"skipping undecodable document %v: %v\", cursor.Current.Lookup(\"_id\"), err)\n}","preventionTips":["Keep _id values as string/ObjectId/int types","Audit collections for exotic _id BSON types before reading with mongodbio","Keep the mongo-driver up to date","Filter out incompatible documents during migration"],"tags":["mongodb","go","bson","decoding"],"backgroundTag":"bson-decode-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}