Goal: find all strings in the 'type' field that start with 'Dog' or 'dog'
Case insensitive searches can be done on fields case insensitive indexes. However, it matches the whole field. For example ...
{ type: 'dog' }
... would work if the value of the field were 'Dog' or 'Dog'. However, if the value of the field is 'Dog is nice' there will be no matches.
To satisfy the 'starts with' condition you can use $regex. In this case you do not need an index. You must include $options: 'i' for case insensitive and 'm to use ahncors.
i: Case insensitivity to match upper and lower cases.
m: For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string.
{ type: { $regex: '^dog', $options: 'im' }}
if you are using $regex it does not appear to use the index and queries are still case sensitive.
Goal: find all strings in the 'type' field that start with 'Dog' or 'dog'
Case insensitive searches can be done on fields case insensitive indexes. However, it matches the whole field. For example ...
... would work if the value of the field were 'Dog' or 'Dog'. However, if the value of the field is 'Dog is nice' there will be no matches.
To satisfy the 'starts with' condition you can use
$regex
. In this case you do not need an index. You must include$options: 'i'
for case insensitive and'm
to use ahncors.$regex
it does not appear to use the index and queries are still case sensitive.$regex
Solution
{ type: { '$regex': '^dog', '$options': 'im' } }
Ref
Case Insensitive Indexes
Regular Expressions Cheat Sheet
Collation Locales and Default Parameters
Collation
$regex
Examples
Start with string
End with string
Contains string
Doesn't Contains string