{"record":{"id":"1e5897e109345e89","repo":"bytebase/bytebase","slug":"failed-to-get-index-schema-of-collection-s","errorCode":null,"errorMessage":"failed to get index schema of collection %s","messagePattern":"failed to get index schema of collection (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mongodb/sync.go","lineNumber":175,"sourceCode":"\t\t}\n\t\tdataSize64, err := convertEmptyInterfaceToInt64(dataSize)\n\t\tif err != nil {\n\t\t\tslog.Debug(\"Failed to convert dataSize to int64\", slog.Any(\"dataSize\", dataSize))\n\t\t}\n\n\t\ttotalIndexSize, ok := commandResult[\"totalIndexSize\"]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get totalIndexSize from collStats command result\")\n\t\t}\n\t\ttotalIndexSize64, err := convertEmptyInterfaceToInt64(totalIndexSize)\n\t\tif err != nil {\n\t\t\tslog.Debug(\"Failed to convert totalIndexSize to int64\", slog.Any(\"totalIndexSize\", totalIndexSize))\n\t\t}\n\n\t\t// Get collection indexes.\n\t\tindexes, err := getIndexes(ctx, collection)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrapf(err, \"failed to get index schema of collection %s\", collectionName)\n\t\t}\n\t\tschemaMetadata.Tables = append(schemaMetadata.Tables, &storepb.TableMetadata{\n\t\t\tName:      collectionName,\n\t\t\tRowCount:  count,\n\t\t\tDataSize:  dataSize64,\n\t\t\tIndexSize: totalIndexSize64,\n\t\t\tIndexes:   indexes,\n\t\t})\n\t}\n\n\tfor _, viewName := range viewNames {\n\t\tschemaMetadata.Views = append(schemaMetadata.Views, &storepb.ViewMetadata{Name: viewName})\n\t}\n\n\treturn &storepb.DatabaseSchemaMetadata{\n\t\tName:    d.databaseName,\n\t\tSchemas: []*storepb.SchemaMetadata{schemaMetadata},\n\t}, nil","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mongodb/sync.go#L157-L193","documentation":"Raised during MongoDB schema sync: the helper getIndexes, which runs the listIndexes command against a collection, failed while building the TableMetadata for that collection. The wrapped error typically carries a driver failure — collection dropped mid-sync, insufficient privileges, or a connection loss to the MongoDB instance — aborting the whole SyncDBSchema for the database.","triggerScenarios":"ListIndexes on a specific collection fails — typically the sync user lacks listIndexes privilege on that collection, the collection was dropped concurrently, or the server connection failed at that point.","commonSituations":"Least-privilege user with find-only access (listIndexes denied); a concurrent migration dropping the collection while sync runs; special namespaces (views, system.*) where index listing is restricted.","solutions":["Grant the sync user listIndexes (built-in read role includes it) on the target database.","Confirm the collection still exists: db.<name>.getIndexes() as the sync user; re-run sync if it was dropped mid-scan.","Read the wrapped error for the precise driver/MongoDB code to distinguish auth vs connectivity vs namespace issues.","Exclude or skip the offending namespace if it is a view or system collection not needed in metadata."],"exampleFix":"// before\nrole limited to {find: targetdb}\n// after\ndb.grantRolesToUser(\"bytebase_user\", [{ role: \"read\", db: \"targetdb\" }])  // includes listIndexes","handlingStrategy":"retry","validationCode":"// pre-check index listing permission\nif _, err := db.RunCommand(ctx, bson.D{{Key:\"listIndexes\", Value:\"probeCollection\"}}).Raw(); err != nil {\n  return fmt.Errorf(\"sync user lacks listIndexes: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"_, err := syncDBSchema(ctx)\nif err != nil {\n  var idxErr *IndexSchemaError\n  if errors.As(err, &idxErr) {\n    slog.Error(\"index sync failed\", slog.String(\"collection\", idxErr.Collection), slog.Any(\"err\", err))\n    // check collection still exists, then retry\n  }\n}","preventionTips":["Grant the read role (includes listIndexes) to the sync user","Avoid running DDL drops concurrently with schema syncs","Skip views and system namespaces — they have no listable indexes","Re-run the sync after concurrent migrations; failures there are often transient"],"tags":["mongodb","sync","indexes","permissions"],"backgroundTag":"database-query-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}