Remove case sensitivity in meteor / mongodb

Hello!

Well, basically what the title says!

test.insert({
  'something' : 'PlsDontBeCaseSensitive'
})

So how id like this to be solved is either, force the string to always be lowercase prior to insert.

OR

Make this one dont care about upper/lowercase

grab: function () {
  var something = 'ThisIsCool'
  return test.find({page: thisiscool});
}  ((You get the point!))

You should take that up with the developers at MongoDB. Meteor just follows their standards.

In the meantime, you’ll need to code for this requirement.

test.insert({
  'something' : SomeUnknownString.toLower()
})

Thank you! That was exactly what i was looking for. I do not want meteor to completely remove case sensitivity, maybe it came out that way. I just wanted a way to solve my problem. Thanks!

you can also do this:

Meteor.publish("search", function (query) {
  var re = new RegExp(query, 'i');
  return Test.find({"something": {$regex: re}});

});
1 Like

Here is the bug in MongoDB’s tracker: https://jira.mongodb.org/browse/SERVER-90

It was open 5 years ago :frowning:

You should really have something like this:

var escapeRegex = function (oldText) {
    "use strict";
    var sre = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '^', '\\', '$'].join('|\\') + ')', 'g');
    return oldText.replace(sre, '\\$1');
};

Add ^ and $ to the beginning and end so you match only that, and escape their query:

Meteor.publish("search", function (query) {
  var re = new RegExp('^' + escapeRegex(query) + '$', 'i');
  return Test.find({"something": {$regex: re}});    
});