0

I need to perform a case-insensitive find. But I am getting a case-insensitive "like" returned, where things that are "like" my string match

I can overcome this in the console or tool by adding ^ and $ - however I cannot figure out how to do that in code, when Im passing a var into the query.

module.exports.getCollege = function( name, callback ) {
    
    const query = [
        {
            $match: {
                'name': { $regex: name, $options: 'i' }
            }
        },
        {
            $project: {
                'address': 1,
                'name': 1
            }
        }
    ];

    College.aggregate( query ).exec( callback );

}

This returns

University of Michigan (object), University of Michigan Flint campus (object)

I just want University of Michigan (object)

How do I get a LITERAL match, but case insensitive???

+++++++++++++++++++++++++++

Response to D. SM - "use collation..."

enter image description here

Returns correct because string is correct..

However - case insensitive not working enter image description here

j-p
  • 3,698
  • 9
  • 50
  • 93
  • Here's a good example using a case insenstive index. As @D.SM suggests. It uses find() but aggregate can similarly declare a collation to use. https://stackoverflow.com/a/40914826/13885615 – GitGitBoom Aug 17 '20 at 15:42

1 Answers1

0

How do I get a LITERAL match, but case insensitive???

Specify collation in your query and use $eq instead of $regex.

D. SM
  • 13,584
  • 3
  • 12
  • 21