is there a way to query for a case-insensitive value on mongo without using map/reduce?
The aggregation framework was introduced in mongodb 2.2 . You can use the string operator "$strcasecmp" to make a case-insensitive comparison between strings. It's more recommended and easier than using regex.
As mentioned by @natac13 and @007_jb mongo shell is an interactive javascript interpreter and hence it is also case-sensitive.
Mongodb supports case insensitive indexing now.
Definition. $not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression> . This includes documents that do not contain the field .
Suppose you have document that contains tag
field and you want search on it
Tags
{
tag,
...
}
First option is use regex(but it work slow as @RestRisiko said):
db.tags.find( { "tag" : { "$regex" : "C#", "$options" : "-i" } })
Second option is create another, lower case field( and in mongodb it best way):
Tags
{
tag,
tagLower,
..
}
And use find
as usual:
db.tags.find( { "tagLower" : "c#"})
It will work faster, because above code can use index for search.
You have to normalize the data to be queried. Using a regular expression for case-insensitive search might work as well it won't use indexes. So your only option is to normalize. If you need to preserve the original state then you need to denormalize the data and store the normalized values in a dedicated column of the document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With