techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.74k stars 482 forks source link

Wrong result while quering by $lt/lte/gte with a large numeric string property #910

Open qiangbro opened 2 years ago

qiangbro commented 2 years ago

my code:

    let t;

    let alarms = [];
    for (let i = 0; i < 80000; i++) {
        let id = "74590869096" + (907836 + i);
        alarms.push({id, name: 'hello' + i});
    }

    var db = new loki('exampleDB');
    var alarmCollection = db.addCollection('myAlarms', {unique: ['id']});

    t = Date.now();
    alarms.forEach(obj => alarmCollection.insert(obj));
    console.log(`完成插入 耗时${(Date.now() - t) / 1000}s alarmCollection.count=${alarmCollection.count()}`);

    let example = alarmCollection.chain().compoundsort(['id']).offset(50000).limit(1).data()[0];
    let exampleId = example.id;

    console.log('exampleId', exampleId)

    t = Date.now();
    console.log('count jlt exampleId by lokijs',  alarmCollection.count({id: {$jlt: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count jlte exampleId by lokijs', alarmCollection.count({id: {$jlte: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count jgt exampleId by lokijs',  alarmCollection.count({id: {$jgt: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);

    t = Date.now();
    console.log('count lt exampleId by js',    alarms.filter(a => (a.id < exampleId)).length, `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count lte exampleId by js',    alarms.filter(a => (a.id <= exampleId)).length, `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count gt exampleId by js',    alarms.filter(a => (a.id > exampleId)).length, `, 耗时${(Date.now() - t) / 1000}s`);

    t = Date.now();
    console.log('count lt exampleId by lokijs',  alarmCollection.count({id: {$lt: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count lte exampleId by lokijs', alarmCollection.count({id: {$lte: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);
    t = Date.now();
    console.log('count gt exampleId by lokijs',  alarmCollection.count({id: {$gt: exampleId}}), `, 耗时${(Date.now() - t) / 1000}s`);

result:

完成插入 耗时0.101s alarmCollection.count=80000
exampleId 74590869096957836
count jlt exampleId by lokijs 50000 , 耗时0.005s
count jlte exampleId by lokijs 50001 , 耗时0.005s
count jgt exampleId by lokijs 29999 , 耗时0.004s
count lt exampleId by js 50000 , 耗时0.002s
count lte exampleId by js 50001 , 耗时0.002s
count gt exampleId by js 29999 , 耗时0.002s
count lt exampleId by lokijs 49997 , 耗时0.018s
count lte exampleId by lokijs 50012 , 耗时0.015s
count gt exampleId by lokijs 29988 , 耗时0.016s

as we can see, count lte exampleId shoud be 50001, es6 and $jlte return correct result, but lokijs's $lte returns 50012.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.