{"record":{"id":"69d2b609f9b5c779","repo":"bytebase/bytebase","slug":"cannot-get-size-from-collstats-command-result","errorCode":null,"errorMessage":"cannot get size from collStats command result","messagePattern":"cannot get size from collStats command result","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mongodb/sync.go","lineNumber":156,"sourceCode":"\t\t\tcontinue\n\t\t}\n\n\t\tcollection := database.Collection(collectionName)\n\t\tcount, err := collection.EstimatedDocumentCount(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"failed to get estimated document count\")\n\t\t}\n\t\t// Get collection data size and total index size in byte.\n\t\tvar commandResult bson.M\n\t\tif err := database.RunCommand(ctx, bson.D{{\n\t\t\tKey:   \"collStats\",\n\t\t\tValue: collectionName,\n\t\t}}).Decode(&commandResult); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"cannot run collStats command\")\n\t\t}\n\t\tdataSize, ok := commandResult[\"size\"]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get size from collStats command result\")\n\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 {","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mongodb/sync.go#L138-L174","documentation":"After running the collStats aggregation command for a collection during schema sync, the driver reads the \"size\" field from the raw command result map. Because commandResult is an untyped bson.M map, it checks that the key exists. This error is thrown when the collStats result does not contain a \"size\" field at all.","triggerScenarios":"SyncDBSchema runs collStats via database.RunCommand and the decoded result lacks the \"size\" key — e.g. the command returned an error document, a stripped-down response from a compatible service, or the collection disappeared between listing and stats collection.","commonSituations":"Connecting to DocumentDB/FerretDB or older MongoDB versions where collStats output differs; the collection was dropped concurrently by another process; running against a mongos proxy that omits fields; hitting a view instead of a collection.","solutions":["Verify the MongoDB server supports collStats with a size field: run db.collection.stats() manually on the target collection","Check whether the target is a view or special namespace (views do not report dataSize the same way) and skip stats for views","If using a compatible service (DocumentDB/FerretDB), confirm it implements collStats fully or pin a version that does","Add defensive handling so a missing size is logged and treated as 0 instead of failing the whole sync"],"exampleFix":"// before\ndataSize, ok := commandResult[\"size\"]\nif !ok {\n\treturn nil, errors.New(\"cannot get size from collStats command result\")\n}\n// after\ndataSize, ok := commandResult[\"size\"]\nif !ok {\n\tslog.Warn(\"collStats result missing size field\", slog.Any(\"collection\", collectionName))\n\tdataSize = int64(0)\n}","handlingStrategy":"fallback","validationCode":"// pre-check via mongosh or driver before sync\nvar res bson.M\nif err := db.RunCommand(ctx, bson.D{{Key:\"collStats\", Value: coll}}).Decode(&res); err != nil {\n\treturn err\n}\nif _, ok := res[\"size\"]; !ok {\n\treturn fmt.Errorf(\"server does not report size for collection %s\", coll)\n}","typeGuard":"func hasKey(m bson.M, k string) bool {\n\t_, ok := m[k]\n\treturn ok\n}","tryCatchPattern":"if err := driver.SyncDBSchema(ctx); err != nil {\n\tif strings.Contains(err.Error(), \"cannot get size from collStats command result\") {\n\t\t// degrade: proceed without size metadata for this database\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Verify collStats support (db.collection.stats()) on the target service before syncing","Skip statistics for views and special namespaces","Treat size metadata as optional — default to 0 and warn"],"tags":["mongodb","collstats","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-14T05:17:10.506Z"}