litedb-org/LiteDB · error · LiteException

0

0

Error message

It's not possible use AutoId={autoId} because '{snapshot.CollectionName}' collection contains not only numbers in _id index ({lastId}).

What it means

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).

Source

Thrown at LiteDB/Engine/Engine/Sequence.cs:27

{
    public partial class LiteEngine
    {
        /// <summary>
        /// Get lastest value from a _id collection and plus 1 - use _sequence cache
        /// </summary>
        private BsonValue GetSequence(Snapshot snapshot, BsonAutoId autoId)
        {
            var next = _sequences.AddOrUpdate(snapshot.CollectionName, (s) =>
            {
                var lastId = this.GetLastId(snapshot);

                // emtpy collection, return 1
                if (lastId.IsMinValue) return 1;

                // if lastId is not number, throw exception
                if (!lastId.IsNumber)
                {
                    throw new LiteException(0, $"It's not possible use AutoId={autoId} because '{snapshot.CollectionName}' collection contains not only numbers in _id index ({lastId}).");
                }

                // return nextId
                return lastId.AsInt64 + 1;
            },
            (s, value) =>
            {
                // update last value
                return value + 1;
            });

            return autoId == BsonAutoId.Int32 ?
                new BsonValue((int)next) :
                new BsonValue(next);
        }

        /// <summary>
        /// Update sequence number with new _id passed by user, IF this number are higher than current last _id

View on GitHub (pinned to f906a5f850)

Solutions

  1. Provide an explicit numeric _id on each insert instead of relying on auto-id.
  2. Migrate existing documents to use numeric _ids if auto-increment is required.
  3. Set BsonAutoId to None or a type consistent with existing data, and manage _id generation yourself.

Example fix

// before
var col = db.GetCollection<Doc>("docs", BsonAutoId.Int32);
col.Insert(new Doc { Name = "x" }); // collection has string _ids

// after
var col = db.GetCollection<Doc>("docs", BsonAutoId.None);
col.Insert(new Doc { Id = Guid.NewGuid().ToString(), Name = "x" });
Defensive patterns

Strategy: validation

Validate before calling

// Before relying on auto-id, check that existing _ids are numeric
var sample = col.Query().Select("$.ToString()").FirstOrDefault();
// or check BsonAutoId compatibility before inserts
if (autoId != BsonAutoId.None)
{ /* verify collection _id type is numeric via a probe query */ }

Try / catch

try { col.Insert(doc); }
catch (LiteException ex) when (ex.Message.Contains("contains not only numbers in _id"))
{ /* switch to explicit _id or BsonAutoId.None */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/3081e1a9aeee31c7. Report an issue: GitHub.