{"record":{"id":"257b33facbf3792a","repo":"bytebase/bytebase","slug":"cannot-get-index-key-from-index-info","errorCode":null,"errorMessage":"cannot get index key from index info","messagePattern":"cannot get index key from index info","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mongodb/sync.go","lineNumber":220,"sourceCode":"\t}\n\tindexMap := make(map[string]*storepb.IndexMetadata)\n\tdefer indexCursor.Close(ctx)\n\tfor indexCursor.Next(ctx) {\n\t\tvar indexInfo bson.M\n\t\tif err := indexCursor.Decode(&indexInfo); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"failed to decode index info\")\n\t\t}\n\t\tname, ok := indexInfo[\"name\"]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get index name from index info\")\n\t\t}\n\t\tindexName, ok := name.(string)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot cinvert index name to string\")\n\t\t}\n\t\tkey, ok := indexInfo[\"key\"]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get index key from index info\")\n\t\t}\n\t\texpression, err := json.Marshal(key)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"cannot marshal index key to json\")\n\t\t}\n\t\tunique := false\n\t\tif u, ok := indexInfo[\"unique\"]; ok {\n\t\t\tunique, ok = u.(bool)\n\t\t\tif !ok {\n\t\t\t\treturn nil, errors.New(\"cannot convert unique to bool\")\n\t\t\t}\n\t\t}\n\n\t\tif _, ok := indexMap[indexName]; !ok {\n\t\t\tindexMap[indexName] = &storepb.IndexMetadata{\n\t\t\t\tName:   indexName,\n\t\t\t\tUnique: unique,\n\t\t\t}","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mongodb/sync.go#L202-L238","documentation":"getIndexes reads the \"key\" field of each index document — the key specification (e.g. {field: 1, other: -1}) — from the decoded bson.M. This error is thrown when the index document lacks a \"key\" field entirely. Every MongoDB index document must contain a key spec, so its absence means the server returned malformed index metadata.","triggerScenarios":"SyncDBSchema → getIndexes: a listIndexes document has a name but no \"key\" field, typically from a compatibility layer, a mock, or a corrupted catalog entry.","commonSituations":"DocumentDB/FerretDB or proxy endpoints emitting incomplete index documents; hand-written test fixtures missing the key field; corrupted index catalog entries after failed builds.","solutions":["Run db.collection.getIndexes() and confirm every returned document contains a key field","Drop and rebuild malformed indexes on the affected collection if the server catalog is corrupted","Verify the target service fully implements the listIndexes contract; if using a proxy or emulation layer, test against real MongoDB to isolate it","Log the offending index document and skip it with a warning instead of failing the full sync"],"exampleFix":"// before\nkey, ok := indexInfo[\"key\"]\nif !ok {\n\treturn nil, errors.New(\"cannot get index key from index info\")\n}\n// after\nkey, ok := indexInfo[\"key\"]\nif !ok {\n\tslog.Warn(\"index info missing key spec\", slog.Any(\"indexName\", indexName))\n\tcontinue\n}","handlingStrategy":"validation","validationCode":"cur, _ := db.Collection(coll).Indexes().List(ctx)\nfor cur.Next(ctx) {\n\tvar info bson.M\n\tcur.Decode(&info)\n\tif _, ok := info[\"key\"]; !ok {\n\t\treturn fmt.Errorf(\"index %v missing key spec\", info[\"name\"])\n\t}\n}","typeGuard":"func indexHasKey(info bson.M) (bson.M, bool) {\n\tkey, ok := info[\"key\"].(bson.M)\n\treturn key, ok\n}","tryCatchPattern":"if err := driver.SyncDBSchema(ctx); err != nil {\n\tif strings.Contains(err.Error(), \"cannot get index key from index info\") {\n\t\t// log offending index document and skip the collection\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Confirm getIndexes() output always contains key fields on the target service","Drop/rebuild corrupted indexes found via db.collection.getIndexes()","Use genuine MongoDB in CI to catch compatible-service divergences early"],"tags":["mongodb","indexes","schema-sync"],"backgroundTag":"unexpected-response-shape","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}