question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTime stohastic index use

See original GitHub issue

Happyness,

This issue, it is in the LiteDB since the 3.x series, and still not fixed in the newest version.

The indexed Min Max values are stohastic. After insert in the same session they are ok, but after reopening the database (program restart) they returning UTC. The filtering looks good, but i am not sure if index is used there.

Here is a test code demonstrating the problem.

using System;
using System.IO;
using System.Linq;
using LiteDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LiteDBDateTimeIndexMinMax
{
    [TestClass]
    public class LiteDBDateTimeIndexMinMaxUnitTest
    {
        [TestMethod]
        public void MinMaxStandardDateTime()
        {
            File.Delete("test.litedb");

            LiteDatabase db1 = new LiteDatabase("test.litedb");
            LiteCollection<DateTimeTest> coll1 = db1.GetCollection<DateTimeTest>();
            coll1.EnsureIndex(x => x.dt);

            coll1.Insert(new DateTimeTest() { DateTimeTestID = 1, dt = new DateTime(2018, 02, 22, 0, 0, 0) });
            coll1.Insert(new DateTimeTest() { DateTimeTestID = 2, dt = new DateTime(2018, 02, 22, 23, 59, 59) });

            MinMaxCommon(coll1);

            db1.Dispose();

            LiteDatabase db2 = new LiteDatabase("test.litedb");
            LiteCollection<DateTimeTest> coll2 = db2.GetCollection<DateTimeTest>();

            MinMaxCommon(coll2);

            coll2.Insert(new DateTimeTest() { DateTimeTestID = 3, dt = new DateTime(2018, 02, 21, 23, 59, 59) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 4, dt = new DateTime(2018, 02, 23, 0, 0, 0) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 5, dt = new DateTime(2018, 02, 22, 0, 0, 1) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 6, dt = new DateTime(2018, 02, 22, 23, 59, 58) });

            MinMaxCommon(coll2);

            db2.Dispose();

            LiteDatabase db3 = new LiteDatabase("test.litedb");
            LiteCollection<DateTimeTest> coll3 = db3.GetCollection<DateTimeTest>();

            MinMaxCommon(coll3);

        }

        private void MinMaxCommon(LiteCollection<DateTimeTest> coll)
        {
            var searchdatetime = new DateTime(2018, 02, 22, 0, 0, 10);

            var min = coll.Min(x => x.dt).AsDateTime;
            var max = coll.Max(x => x.dt).AsDateTime;

            var smaller = coll.Find(x => x.dt < searchdatetime);
            var greater = coll.Find(x => x.dt > searchdatetime);

            var all = coll.FindAll().ToList();

            var linqmin = all.Min(x => x.dt);
            var linqmax = all.Max(x => x.dt);

            Assert.AreEqual(min, linqmin);
            Assert.AreEqual(max, linqmax);
        }

        public class DateTimeTest
        {
            public int DateTimeTestID { get; set; }
            public DateTime? dt { get; set; }
            public string mittomen { get; set; }
        }
    }
}

Joy and Harmony!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mbdavidcommented, Mar 10, 2018

Hi @MiklosPathy, partial fixed. Now, index key are converted in Local date king. But, LiteDB has UTC parameter that are not used in this case. I will review this double serialize/deserialize bson implementation to use an single option.

0reactions
MiklosPathycommented, May 14, 2021

Hi agan,

I ran the test with 5.10 and found exactly the same problem.

Here is the test adjusted for 5.x

using System;
using System.IO;
using System.Linq;
using LiteDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LiteDB.Tests
{
    [TestClass]
    public class LiteDBDateTimeIndexMinMaxUnitTest
    {
        /// <summary>
        /// Min Max datetime indexelési hiba tesztelése. LiteDB 3.15-ben javítva lett a visszajelzésem után.
        /// </summary>
        [TestMethod]
        public void MinMaxStandardDateTime()
        {
            File.Delete("test.litedb");
            File.Delete("test-log.litedb");

            LiteDatabase db1 = new LiteDatabase("test.litedb");
            ILiteCollection<DateTimeTest> coll1 = db1.GetCollection<DateTimeTest>();
            coll1.EnsureIndex(x => x.dt);

            coll1.Insert(new DateTimeTest() { DateTimeTestID = 1, dt = new DateTime(2018, 02, 22, 0, 0, 0) });
            coll1.Insert(new DateTimeTest() { DateTimeTestID = 2, dt = new DateTime(2018, 02, 22, 23, 59, 59) });

            MinMaxCommon(coll1);

            db1.Dispose();

            LiteDatabase db2 = new LiteDatabase("test.litedb");
            ILiteCollection<DateTimeTest> coll2 = db2.GetCollection<DateTimeTest>();

            MinMaxCommon(coll2);

            coll2.Insert(new DateTimeTest() { DateTimeTestID = 3, dt = new DateTime(2018, 02, 21, 23, 59, 59) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 4, dt = new DateTime(2018, 02, 23, 0, 0, 0) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 5, dt = new DateTime(2018, 02, 22, 0, 0, 1) });
            coll2.Insert(new DateTimeTest() { DateTimeTestID = 6, dt = new DateTime(2018, 02, 22, 23, 59, 58) });

            MinMaxCommon(coll2);

            db2.Dispose();

            LiteDatabase db3 = new LiteDatabase("test.litedb");
            ILiteCollection<DateTimeTest> coll3 = db3.GetCollection<DateTimeTest>();

            MinMaxCommon(coll3);

        }

        private void MinMaxCommon(ILiteCollection<DateTimeTest> coll)
        {
            var searchdatetime = new DateTime(2018, 02, 22, 0, 0, 10);

            var min = coll.Min(x => x.dt);
            var max = coll.Max(x => x.dt);

            var smaller = coll.Find(x => x.dt < searchdatetime);
            var greater = coll.Find(x => x.dt > searchdatetime);

            var all = coll.FindAll().ToList();

            var linqmin = all.Min(x => x.dt);
            var linqmax = all.Max(x => x.dt);

            Assert.AreEqual(min, linqmin);
            Assert.AreEqual(max, linqmax);
        }

        public class DateTimeTest
        {
            public int DateTimeTestID { get; set; }
            public DateTime? dt { get; set; }
            public string mittomen { get; set; }
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Stochastic Oscillator: What It Is, How It Works, How To ...
A stochastic oscillator is a momentum indicator comparing a particular closing price of a security to a range of its prices over a...
Read more >
Stochastics: An Accurate Buy and Sell Indicator
Easy to understand and highly accurate, stochastics is a technical indicator that shows when a stock has moved into an overbought or oversold...
Read more >
Algorithmic Trading with Stochastic Oscillator in Python
Stochastic Oscillator is a momentum-based leading indicator that is widely used to identify whether the market is in the state of overbought or ......
Read more >
Beginner's Guide to Trading with the Stochastic Oscillator ...
The Stochastic Oscillator is a great indicator to add to your trading algorithm. On its own, it can be used for momentum strategies, ......
Read more >
Fast Stochastic Oscillator
The Fast Stochastic Oscillator is a momentum indicator that shows the location of the close relative to the high-low range over a set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found