{"record":{"id":"3982142c16874766","repo":"litedb-org/LiteDB","slug":"0-398214","errorCode":"0","errorMessage":"This collection has no more space for new indexes","messagePattern":"This collection has no more space for new indexes","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/Pages/CollectionPage.cs","lineNumber":203,"sourceCode":"\n        public VectorIndexMetadata GetVectorIndexMetadata(string name)\n        {\n            return _vectorIndexes.TryGetValue(name, out var metadata) ? metadata : null;\n        }\n\n        /// <summary>\n        /// Insert new index inside this collection page\n        /// </summary>\n        public CollectionIndex InsertCollectionIndex(string name, string expr, bool unique)\n        {\n            if (_indexes.ContainsKey(name) || _vectorIndexes.ContainsKey(name))\n            {\n                throw LiteException.IndexAlreadyExist(name);\n            }\n\n            var totalLength = this.GetSerializedLength(CollectionIndex.GetLength(name, expr), 0);\n\n            if (_indexes.Count == 255 || totalLength >= P_INDEXES_COUNT) throw new LiteException(0, $\"This collection has no more space for new indexes\");\n\n            var slot = (byte)(_indexes.Count == 0 ? 0 : (_indexes.Max(x => x.Value.Slot) + 1));\n\n            var index = new CollectionIndex(slot, 0, name, expr, unique);\n\n            _indexes[name] = index;\n\n            this.IsDirty = true;\n\n            return index;\n        }\n\n        public (CollectionIndex Index, VectorIndexMetadata Metadata) InsertVectorIndex(string name, string expr, ushort dimensions, VectorDistanceMetric metric)\n        {\n            if (_indexes.ContainsKey(name) || _vectorIndexes.ContainsKey(name))\n            {\n                throw LiteException.IndexAlreadyExist(name);\n            }","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/Pages/CollectionPage.cs#L185-L221","documentation":"Thrown by CollectionPage.InsertCollectionIndex when a collection cannot accept another standard index. Two conditions trigger it: the collection already has 255 indexes (the byte slot limit), or the serialized length of the new index entry plus existing entries exceeds P_INDEXES_COUNT — the fixed byte region reserved for index metadata inside the single collection header page. Each CollectionIndex occupies space proportional to its name and expression string.","triggerScenarios":"Calling EnsureIndex / CreateIndex (or db.Execute with CREATE INDEX) on a collection that is at the 255-index hard cap, or whose header page has insufficient free bytes for the new index metadata due to long index names/expressions.","commonSituations":"Programmatically generating many indexes (e.g. one per dynamic field); using very long index names or complex expression strings that consume the reserved byte budget; accumulating indexes over time without cleanup.","solutions":["Drop unused indexes before creating new ones (db.GetCollection(name).DropIndex(...)).","Shorten index names and/or expression strings to fit in the reserved header region.","If you genuinely need more than 255 indexes, reconsider the data model — LiteDB collections are capped by design.","Audit existing indexes with GetIndexes() before adding more."],"exampleFix":"// before — too many indexes\ncol.EnsureIndex(\"idx_256\", \"$ field256\"); // throws\n\n// after — drop unused, then add\ncol.CreateIndex(\"idx_256\", \"$ field256\");\n// or remove a stale one first:\ncol.DropIndex(\"idx_old\");","handlingStrategy":"validation","validationCode":"const int MAX_INDEXES = 255;\n\npublic void CreateIndexSafely(ILiteCollection<BsonDocument> col, string name, string expr, bool unique)\n{\n    var existing = col.GetIndexes().Count();\n    if (existing >= MAX_INDEXES)\n        throw new InvalidOperationException($\"Collection '{col.Name}' already has {existing} indexes (max {MAX_INDEXES}). Drop one first.\");\n    col.CreateIndex(name, BsonExpression.Create(expr), unique);\n}","typeGuard":"static bool CanAddIndex(ILiteCollection<BsonDocument> col) =>\n    col.GetIndexes().Count() < 255;","tryCatchPattern":"try\n{\n    col.EnsureIndex(name, expr);\n}\ncatch (LiteException ex) when (ex.Message.Contains(\"no more space for new indexes\"))\n{\n    // Drop a stale index or report to the user that the collection is at capacity.\n    throw new InvalidOperationException(\"Collection has reached its index limit (255) or header space is full.\", ex);\n}","preventionTips":["Audit indexes with GetIndexes() before creating new ones.","Drop unused indexes to free slots and header bytes.","Use short, descriptive index names.","Avoid auto-generating one index per dynamic field."],"tags":["index","collection-page","schema-limit","litedb-engine"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}