techfort / LokiJS

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

$regex Query improperly handles the 'g' modifier #902

Closed cepm-nate closed 2 years ago

cepm-nate commented 2 years ago

Given this dataset:

// Given this dataset:
[
    {
        "Material": "NHTCOH"
    },
    {
        "Material": "NHTCOI",
    },
    {
        "Material": "NHTCOK"
    },
    {
        "Material": "NHTCOM"
    },
    {
        "Material": "NHTCOR"
    },
    {
        "Material": "NHTCOWPI"
    }
]

The following code demonstrates an issue with the 'g' option

// This finds only THREE of the records
results = coll.find({Material: {'$regex':['nhtco.*', 'gi']}});

// This one ALSO finds only THREE (even with .* on front and back)
results = coll.find({Material: {'$regex':['.*nhtco.*', 'gi']}});

// This one correctly finds all SIX (removed 'g')
results = coll.find({Material: {'$regex':['.*nhtco.*', 'i']}});

// This one also finds all SIX
results = coll.find({Material: { '$regex':/nhtco.*/i }});

// This one finds only THREE (added the 'g')
results = coll.find({Material: { '$regex':/nhtco.*/gi }});

The last two results might be due to the way /regex/ does not serialize. But that does not excuse the first few results. In my case removing 'g' works, as I don't need multiple matching. But for someone who does, take heed!

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.