LiteDB and MongoDB return time format is different
Hi, mbdavid
LiteDB and MongoDB stores times in UTC by default.
The time for LiteDB to return is locally relevant, but MongoDB return UTC format by default.
Will the future version of LiteDB be consistent with mongo?
Test as follows:
public class Person
{
public int Id { get; set; }
public string Fullname { get; set; }
public DateTime Birthday { get; set; }
}
LiteDB:
var col = db.GetCollection<Person>("Person");
var p = new Person {Fullname = "John", Birthday = DateTime.Now.Add(TimeSpan.FromDays(1))};
col.Insert(p);
var record = col.FindAll().ToList().FirstOrDefault(x => x.Fullname=="John");
Assert.IsTrue(record.Birthday.Kind == DateTimeKind.Local); // test pass
MongoDB(CSharpDriver):
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<Person>("bar");
var p = new Person {Id=1, Fullname = "John", Birthday = DateTime.Now.Add(TimeSpan.FromDays(1))};
collection.InsertOne(p);
var record = collection.Find(x => x.Id == 1).FirstOrDefault();
Assert.IsTrue(record.Birthday.Kind == DateTimeKind.Utc); // test pass
Reference article:
https://docs.mongodb.com/manual/tutorial/model-time-data/
https://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/
Hi @codeyu, I will take a look on this. But now I think it´s complicate to change this by default. Maybe add as parameter in some level (by connection, mapper, ...)
Hi @mbdavid, can you include a fix for this in the next release? Thanks.
I'm interested in using utc too. LiteEngine has a parameter for utc, but LiteDatabase has not.
Can we add this parameter for consistency?
Hi @ViceIce, if you are talking about v5 this will be checked. EngineSettings are not same as ConnectionString in v5. UTC parameter is in Engine level (and not in ConnectionString level).
@mbdavid I know, LiteDataBase simply creates LiteEngine with utc=false. So it would be nice to allow overriding this behavior. Or allow passing LiteEngine instead of IDiskService to ctor.
@ViceIce, are you using v4 or v5? In v5 there is no more IDiskService
I'm using v4, because v5 is alpha, and my current timeframe is a release at end of the year
v4 support utc=true|false in connection string. You can see here:
https://github.com/mbdavid/LiteDB/blob/v4/LiteDB/Utils/ConnectionString.cs#L63
I know the connection string constructor, but i would like to reuse my LiteEngine instance.
Most of my solution uses generic configuration based documents, but for some parts i would like to use the strong typed LiteDatabase and LiteCollection. So for concurrency it would be more performant to reuse the LiteEngine.
So my current workaround would be to create the LiteCollection directly without using LiteDatabase.
I'm using FileMode.Exclusive, because i have a single process accessing the files.