Reputation: 1
for example i have data like this in my database in mycoll as a collection
{id=1,Name:"xxx",rank:1},
{id=2,Name:"Y",rank:2},
{id=3,Nae:"Zzz",rank:3}
while writing query i don't know name is written as Name,i have simple passed as name like this as i show
db. mycoll .find({Name:"xxx"})
hence this query returns me 0 records. Because field path which i gave doesn't match with records in database .help me to write query ignoring case sensitive in field path.
https://docs.mongodb.com/manual/core/index-case-insensitive/
Upvotes: 0
Views: 110
Reputation: 16321
To use a case insensitive index on a collection with no default collation, create an index with a collation and set the strength parameter to 1
or 2
like this:
db.mycoll.find({ Name:"xxx" }).collation({ locale: 'en', strength: 1 });
With regards to the value for strength
:
With strength: 1
, collation performs comparisons of the base characters only, ignoring other differences such as diacritics and case. This level is also known as the primary level of comparison.
Typically, the primary level is used to denote differences between base characters (for example, "a" < "b"). It is the strongest difference. For example, dictionaries are divided into different sections by base character.
With strength: 2
, collation performs comparisons up to secondary differences, such as diacritics. That is, collation performs comparisons of base characters (primary differences) and diacritics (secondary differences). Differences between base characters takes precedence over secondary differences. This is also known as the secondary level of comparison.
In the secondary level, accents in the characters are considered secondary differences (for example, "as" < "às" < "at"). Other differences between letters can also be considered secondary differences, depending on the language. A secondary difference is ignored when there is a primary difference anywhere in the strings.
N.B. Check out the official MongoDB docs on Collation document fields as well as the ICU User Guide on comparison levels for more info about the different values you can set for strength.
Upvotes: 1