{"record":{"id":"98c95619b5b153fd","repo":"gofr-dev/gofr","slug":"mongo-w","errorCode":null,"errorMessage":"mongo: %w","messagePattern":"mongo: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/migration/mongo.go","lineNumber":77,"sourceCode":"\t\treturn err\n\t}\n\n\treturn mg.migrator.checkAndCreateMigrationTable(c)\n}\n\nfunc (mg mongoMigrator) getLastMigration(c *container.Container) (int64, error) {\n\tvar (\n\t\tlastMigration int64\n\t\tmigrations    []struct {\n\t\t\tVersion int64 `bson:\"version\"`\n\t\t}\n\t)\n\n\tfilter := make(map[string]any)\n\n\terr := mg.Mongo.Find(context.Background(), mongoMigrationCollection, filter, &migrations)\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"mongo: %w\", err)\n\t}\n\n\t// Identify the highest migration version.\n\tfor _, migration := range migrations {\n\t\tlastMigration = max(lastMigration, migration.Version)\n\t}\n\n\tc.Debugf(\"MongoDB last migration fetched value is: %v\", lastMigration)\n\n\tlm2, err := mg.migrator.getLastMigration(c)\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\n\treturn max(lastMigration, lm2), nil\n}\n\nfunc (mg mongoMigrator) beginTransaction(c *container.Container) transactionData {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/migration/mongo.go#L59-L95","documentation":"Wraps the error from Mongo's Find on the migrations collection when getLastMigration reads all migration records to compute the highest version. Without this list the migrator cannot determine which migrations have already run. The root cause is in the wrapped Mongo error.","triggerScenarios":"mg.Mongo.Find(context.Background(), mongoMigrationCollection, filter, &migrations) fails: collection missing, connection failure, auth error, or decode mismatch between stored docs and the migration struct.","commonSituations":"Wrong database/collection configured; Mongo credentials rotated; documents written by an older gofr version with a different schema causing bson decode errors; Mongo down during startup migrations.","solutions":["Verify Mongo connection settings (host, db, credentials) in container config","Check the wrapped mongo error — auth failure vs connection vs decode","Ensure the migrations collection schema matches what this gofr version expects","Re-run once Mongo is reachable; the query is read-only and safe to retry"],"exampleFix":"// before\nerr := mg.Mongo.Find(ctx, \"gofr_migrations\", filter, &migrations) // mongo: auth failed\n// after\n// fix credentials in config\nMONGO_URI=mongodb://user:correctpass@host:27017/gofr","handlingStrategy":"try-catch","validationCode":"if c.Mongo == nil { return errors.New(\"mongo datasource not configured\") }\nif err := c.Mongo.Ping(context.Background(), nil); err != nil { return fmt.Errorf(\"mongo unreachable: %w\", err) }","typeGuard":null,"tryCatchPattern":"err := migrator.Run(c)\nif err != nil && strings.Contains(err.Error(), \"mongo:\") {\n    log.Printf(\"mongo migration lookup failed, root cause: %v\", errors.Unwrap(err))\n    // auth/connection vs decode: inspect wrapped error before retrying\n}","preventionTips":["Ping Mongo at startup before running migrations","Keep migration document schema consistent across gofr upgrades (test decode first)","Rotate credentials with zero-downtime and update config simultaneously","Pin database/collection names in configuration, not code"],"tags":["mongodb","migration","error-wrapping"],"backgroundTag":"mongo-query-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}