bytebase/bytebase · error
cannot get index key from index info
Error message
cannot get index key from index info
What it means
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.
Source
Thrown at backend/plugin/db/mongodb/sync.go:220
}
indexMap := make(map[string]*storepb.IndexMetadata)
defer indexCursor.Close(ctx)
for indexCursor.Next(ctx) {
var indexInfo bson.M
if err := indexCursor.Decode(&indexInfo); err != nil {
return nil, errors.Wrap(err, "failed to decode index info")
}
name, ok := indexInfo["name"]
if !ok {
return nil, errors.New("cannot get index name from index info")
}
indexName, ok := name.(string)
if !ok {
return nil, errors.New("cannot cinvert index name to string")
}
key, ok := indexInfo["key"]
if !ok {
return nil, errors.New("cannot get index key from index info")
}
expression, err := json.Marshal(key)
if err != nil {
return nil, errors.Wrap(err, "cannot marshal index key to json")
}
unique := false
if u, ok := indexInfo["unique"]; ok {
unique, ok = u.(bool)
if !ok {
return nil, errors.New("cannot convert unique to bool")
}
}
if _, ok := indexMap[indexName]; !ok {
indexMap[indexName] = &storepb.IndexMetadata{
Name: indexName,
Unique: unique,
}View on GitHub (pinned to 1870550677)
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
Example fix
// before
key, ok := indexInfo["key"]
if !ok {
return nil, errors.New("cannot get index key from index info")
}
// after
key, ok := indexInfo["key"]
if !ok {
slog.Warn("index info missing key spec", slog.Any("indexName", indexName))
continue
} Defensive patterns
Strategy: validation
Validate before calling
cur, _ := db.Collection(coll).Indexes().List(ctx)
for cur.Next(ctx) {
var info bson.M
cur.Decode(&info)
if _, ok := info["key"]; !ok {
return fmt.Errorf("index %v missing key spec", info["name"])
}
} Type guard
func indexHasKey(info bson.M) (bson.M, bool) {
key, ok := info["key"].(bson.M)
return key, ok
} Try / catch
if err := driver.SyncDBSchema(ctx); err != nil {
if strings.Contains(err.Error(), "cannot get index key from index info") {
// log offending index document and skip the collection
return nil
}
return err
} Prevention
- 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
When it happens
Trigger: SyncDBSchema → getIndexes: a listIndexes document has a name but no "key" field, typically from a compatibility layer, a mock, or a corrupted catalog entry.
Common situations: DocumentDB/FerretDB or proxy endpoints emitting incomplete index documents; hand-written test fixtures missing the key field; corrupted index catalog entries after failed builds.
Related errors
- cannot convert collection name to string
- cannot get size from collStats command result
- cannot get index name from index info
- cannot cinvert index name to string
- cannot convert unique to bool
AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06).
Data as JSON: /api/errors/257b33facbf3792a.
Report an issue: GitHub.