'MongoDB : How to query or search case-insensitive values

What worked for me was :

async function getByName(name) {
    try {
        const query = { username: { '$regex': `^${name}$`, '$options': 'i' }}
        const user = await collection.findOne(query);
        return user;
    } catch (err) {
        throw err;
    }
}

You may replace the "findOne()" with a regular "find.

Write your thoughts.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source