{"record":{"id":"3081e1fc568700ee","repo":"bytebase/bytebase","slug":"cannot-convert-collection-name-to-string","errorCode":null,"errorMessage":"cannot convert collection name to string","messagePattern":"cannot convert collection name to string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mongodb/sync.go","lineNumber":116,"sourceCode":"\t\tif err := collectionList.Decode(&collection); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"failed to decode collection\")\n\t\t}\n\t\tvar tp string\n\t\tif t, ok := collection[\"type\"]; ok {\n\t\t\tif s, ok := t.(string); ok && s == \"collection\" {\n\t\t\t\ttp = \"collection\"\n\t\t\t}\n\t\t\tif s, ok := t.(string); ok && s == \"view\" {\n\t\t\t\ttp = \"view\"\n\t\t\t}\n\t\t}\n\t\tname, ok := collection[\"name\"]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get collection name from collection info\")\n\t\t}\n\t\tcollectionName, ok := name.(string)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot convert collection name to string\")\n\t\t}\n\t\tswitch tp {\n\t\tcase \"collection\":\n\t\t\tcollectionNames = append(collectionNames, collectionName)\n\t\tcase \"view\":\n\t\t\tviewNames = append(viewNames, collectionName)\n\t\tdefault:\n\t\t\t// Other types like system collections\n\t\t}\n\t}\n\tif err := collectionList.Err(); err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to list collection names\")\n\t}\n\tif err := collectionList.Close(ctx); err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to close collection list\")\n\t}\n\tslices.Sort(collectionNames)\n\tslices.Sort(viewNames)","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mongodb/sync.go#L98-L134","documentation":"During MongoDB schema sync, the driver reads collection info entries returned by the server (from listCollections-style results). Each entry is an untyped map (bson.M), so it asserts that the entry has a \"name\" field and that the value is a string. This error is thrown when the \"name\" key exists but its value cannot be type-asserted to string, meaning the server returned an unexpected shape for the collection name.","triggerScenarios":"SyncDBSchema iterates collection info maps where collection[\"name\"] holds a non-string BSON value (e.g. null, a document, or a custom BSON type) instead of the expected string collection name.","commonSituations":"Corrupted or unusual metadata returned by the MongoDB server; a proxy or shim (e.g. DocumentDB emulation layers, mock servers in tests) that returns collection info in a different shape; BSON decoding quirks where the name field decodes to a non-string type.","solutions":["Inspect the actual collection info returned by running db.runCommand({listCollections:1}) against the same database and check the name field's BSON type","Upgrade the mongo driver and MongoDB server to compatible versions, since this normally never happens with genuine servers","If connecting to a compatible service (DocumentDB, FerretDB), verify it implements listCollections with string name fields; file an issue with the raw collection info payload otherwise"],"exampleFix":"// before\ncollectionName, ok := name.(string)\nif !ok {\n\treturn nil, errors.New(\"cannot convert collection name to string\")\n}\n// after\ncollectionName, ok := name.(string)\nif !ok {\n\treturn nil, errors.Wrapf(errors.New(\"cannot convert collection name to string\"), \"unexpected type %T for collection name\", name)\n}","handlingStrategy":"type-guard","validationCode":"// before calling SyncDBSchema there is no direct pre-check; guard at the data boundary\nfor _, col := range collectionInfos {\n\tif nameVal, present := col[\"name\"]; !present {\n\t\treturn fmt.Errorf(\"collection info missing name\")\n\t} else if _, isStr := nameVal.(string); !isStr {\n\t\treturn fmt.Errorf(\"collection name has unexpected type %T\", nameVal)\n\t}\n}","typeGuard":"func asString(v any) (string, bool) {\n\ts, ok := v.(string)\n\treturn s, ok\n}","tryCatchPattern":"if err := driver.SyncDBSchema(ctx); err != nil {\n\tvar se *errors.Error\n\tif stderrors.As(err, &se) && strings.Contains(se.Error(), \"cannot convert collection name to string\") {\n\t\t// log server response anomaly and skip this database\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Run SyncDBSchema against genuine MongoDB endpoints; verify DocumentDB/FerretDB compatibility before use","Test with db.runCommand({listCollections:1}) to confirm name fields are strings before automating syncs","Keep mongo-driver and server versions aligned"],"tags":["mongodb","type-assertion","schema-sync"],"backgroundTag":"type-mismatch","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"}