{"record":{"id":"3081e1a9aeee31c7","repo":"litedb-org/LiteDB","slug":"0-3081e1","errorCode":"0","errorMessage":"It's not possible use AutoId={autoId} because '{snapshot.CollectionName}' collection contains not only numbers in _id index ({lastId}).","messagePattern":"It's not possible use AutoId=(.+?) because '(.+?)' collection contains not only numbers in _id index \\((.+?)\\)\\.","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/Engine/Sequence.cs","lineNumber":27,"sourceCode":"{\n    public partial class LiteEngine\n    {\n        /// <summary>\n        /// Get lastest value from a _id collection and plus 1 - use _sequence cache\n        /// </summary>\n        private BsonValue GetSequence(Snapshot snapshot, BsonAutoId autoId)\n        {\n            var next = _sequences.AddOrUpdate(snapshot.CollectionName, (s) =>\n            {\n                var lastId = this.GetLastId(snapshot);\n\n                // emtpy collection, return 1\n                if (lastId.IsMinValue) return 1;\n\n                // if lastId is not number, throw exception\n                if (!lastId.IsNumber)\n                {\n                    throw new LiteException(0, $\"It's not possible use AutoId={autoId} because '{snapshot.CollectionName}' collection contains not only numbers in _id index ({lastId}).\");\n                }\n\n                // return nextId\n                return lastId.AsInt64 + 1;\n            },\n            (s, value) =>\n            {\n                // update last value\n                return value + 1;\n            });\n\n            return autoId == BsonAutoId.Int32 ?\n                new BsonValue((int)next) :\n                new BsonValue(next);\n        }\n\n        /// <summary>\n        /// Update sequence number with new _id passed by user, IF this number are higher than current last _id","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/Engine/Sequence.cs#L9-L45","documentation":"Thrown by GetSequence when AutoId is enabled (Int32 or Int64) but the existing _id values in the collection are not all numbers — the latest _id is non-numeric. The auto-id sequence generator can only increment numeric _id values, so a non-number _id (string, Guid, ObjectId) makes auto-increment impossible. This is a LiteException (code 0).","triggerScenarios":"Inserting a document into a collection configured with BsonAutoId.Int32 or Int64 when the collection already contains documents with non-numeric _id values (strings, GUIDs, ObjectIds). Inserting without an explicit _id relies on auto-id, which then fails.","commonSituations":"A collection was initially used with string/Guid _ids, then code switches to auto-id without a schema migration. Mixed inserts where some documents have explicit non-numeric _ids. Changing BsonAutoId on an existing collection that has heterogeneous _id types.","solutions":["Provide an explicit numeric _id on each insert instead of relying on auto-id.","Migrate existing documents to use numeric _ids if auto-increment is required.","Set BsonAutoId to None or a type consistent with existing data, and manage _id generation yourself."],"exampleFix":"// before\nvar col = db.GetCollection<Doc>(\"docs\", BsonAutoId.Int32);\ncol.Insert(new Doc { Name = \"x\" }); // collection has string _ids\n\n// after\nvar col = db.GetCollection<Doc>(\"docs\", BsonAutoId.None);\ncol.Insert(new Doc { Id = Guid.NewGuid().ToString(), Name = \"x\" });","handlingStrategy":"validation","validationCode":"// Before relying on auto-id, check that existing _ids are numeric\nvar sample = col.Query().Select(\"$.ToString()\").FirstOrDefault();\n// or check BsonAutoId compatibility before inserts\nif (autoId != BsonAutoId.None)\n{ /* verify collection _id type is numeric via a probe query */ }","typeGuard":null,"tryCatchPattern":"try { col.Insert(doc); }\ncatch (LiteException ex) when (ex.Message.Contains(\"contains not only numbers in _id\"))\n{ /* switch to explicit _id or BsonAutoId.None */ }","preventionTips":["Do not enable numeric AutoId on collections with non-numeric _ids.","Provide explicit _id values when collection data types are heterogeneous.","Migrate _id types before switching auto-id strategy."],"tags":["engine","auto-id","sequence","schema","lite-exception"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}