Is a case insensitive aggregate match possible in Mongo without using $regex operators?

Viewed 1048

I'm aware of how to use a case insensitive collation with find(), but is there something similar for aggregation? The only case insensitive aggregation operators I've found so far use some variant of $regex, all of which ignore the index, as far as I can tell. Perhaps I am missing something.

I'm hoping for something similar to

[$match: {{name: "Foo"}, collation: {locale: 'en', strength: 2}}]

which would find name == "foo", "FOO", "fOO", etc., but any mechanism to do it with an index would be welcome.

Bonus if there's a Mongoose way to do so!

1 Answers

just create a text index:

db.collection.createIndex({ "name": "text" })

then write an aggregation pipeline like this:

db.collection.aggregate(
    [
        {
            $match: {
                $text: {
                    $search: "Foo"
                }
            }
        }
    ]
)
Related