Remove case sensitivity in meteor / mongodb


#1

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!))

#2

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()
})

#3

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!


#4

you can also do this:

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

});

#5

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

It was open 5 years ago :frowning:


#6

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}});    
});