mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

ID does not increment when inserting #1386

Open 0verflux opened 4 years ago

0verflux commented 4 years ago
class Test
{
    [BsonId]
    public ulong ID { get; set }
    publi string Name { get; set }
}

using ulong as datatype for ID does not increment, and results with an exception.

maxkatz6 commented 4 years ago

Can you provide some sample? Minimal code to run and reproduce the issue.

JensSchadron commented 4 years ago

@FDemin Can you also provide us with some extra information like which version of LiteDB that you're using and which OS that you're trying to run it on?

0verflux commented 4 years ago

having a property name Id works on auto-incrementing however, having a different property name (ID, <Model Name>ID), even having a [BsonId] attribute, auto-increment doesn't work

lbnascimento commented 4 years ago

@FDemin Auto-id only supports types Int32, Int64, ObjectId and Guid. In fact, the BSON spec doesn't even support UInt64 as a valid type for an element in a BSON (our serializer converts it to Int64).

diegosasw commented 4 years ago

Question about this. I've tested it with Id as a long and I can see it creates a json object instead of a number. If I use int, it creates a number. Both seems to auto-increment correctly, but.. how do I get to use the long as a number and not as an object with a number embedded?

See the following snapshot of a sample ran first with long and then with int

Capture

lbnascimento commented 4 years ago

@sunnyatticsoftware LiteDB uses BSON serialization when storing data, which means that, internally, both int32 and int64 are stored in binary form. However, BSON is a superset of JSON – JSON only has a "number" type, while BSON has distinct types for int32, int64, double and decimal. So, in order to preserve the type of the value when converting a BSON to JSON, we use custom formats:

If you want to display only the numerical value of longs, you could do something like this: SELECT INT32($._id), Type, ... FROM events.