gofr-dev/gofr · error
mongo: %w
Error message
mongo: %w
What it means
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.
Source
Thrown at pkg/gofr/migration/mongo.go:77
return err
}
return mg.migrator.checkAndCreateMigrationTable(c)
}
func (mg mongoMigrator) getLastMigration(c *container.Container) (int64, error) {
var (
lastMigration int64
migrations []struct {
Version int64 `bson:"version"`
}
)
filter := make(map[string]any)
err := mg.Mongo.Find(context.Background(), mongoMigrationCollection, filter, &migrations)
if err != nil {
return -1, fmt.Errorf("mongo: %w", err)
}
// Identify the highest migration version.
for _, migration := range migrations {
lastMigration = max(lastMigration, migration.Version)
}
c.Debugf("MongoDB last migration fetched value is: %v", lastMigration)
lm2, err := mg.migrator.getLastMigration(c)
if err != nil {
return -1, err
}
return max(lastMigration, lm2), nil
}
func (mg mongoMigrator) beginTransaction(c *container.Container) transactionData {View on GitHub (pinned to 187eb24962)
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
Example fix
// before err := mg.Mongo.Find(ctx, "gofr_migrations", filter, &migrations) // mongo: auth failed // after // fix credentials in config MONGO_URI=mongodb://user:correctpass@host:27017/gofr
Defensive patterns
Strategy: try-catch
Validate before calling
if c.Mongo == nil { return errors.New("mongo datasource not configured") }
if err := c.Mongo.Ping(context.Background(), nil); err != nil { return fmt.Errorf("mongo unreachable: %w", err) } Try / catch
err := migrator.Run(c)
if err != nil && strings.Contains(err.Error(), "mongo:") {
log.Printf("mongo migration lookup failed, root cause: %v", errors.Unwrap(err))
// auth/connection vs decode: inspect wrapped error before retrying
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- elasticsearch: %w
- failed to refresh MongoDB lock: lock lost or stolen
- errors.Wrap(e.Err, e.Message).Error()
- failed to generate signed URL for %q: %w
- %w: %v
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/98c95619b5b153fd.
Report an issue: GitHub.