0

I am seeing mongo regex query not returning result when the regex searched string is very big, instead its throwing error. I have a scenario where I append lot of names to do a regex and thus my regex search string goes beyond 40000 characters. eg:

db.getCollection('collection').find(
    {"name":{"$regex" :"name1 | name2 | name3", "$options":"-i"}}
)
jrbedard
  • 3,662
  • 5
  • 30
  • 34

2 Answers2

0

Can you explain why you are doing this? The idea of a regex is to create a expression with which you match (multiple) value(s).

example expression:

name\d+

will match on all "namex" vales where x is a decimal.

The idea will be to create a single expression to fullfill your query requirement.

When you want to match on multiple string values you can use $and operator

HoefMeistert
  • 1,190
  • 8
  • 17
  • I am doing a case insensitive search, query is as given below – user7045224 Oct 25 '16 at 07:08
  • db.collection.find( name: { "$regex":"red | white | blue|green " "$options": 'i')}) – user7045224 Oct 25 '16 at 07:10
  • the values string passed to regex can really grow very big and I am seeing mongo throwing exception when the string have characters above 37000 – user7045224 Oct 25 '16 at 07:12
  • This is not where regex is for... If you want to query use the $and operator. db.collection.find({$and : [ { name : "red"}, { name : "blue"} ]}) or the$in operator : db.collection.find({ name : { $in : [ "red", "white", "blue", "green" ]}}) you can create an array for it. It's required you store the value in lower case so the query is a success. As from MongoDB 3.4 there will be case insensitive indexes : https://jira.mongodb.org/browse/SERVER-90 – HoefMeistert Oct 25 '16 at 07:19
  • But my problem is data contains both upper and lower case (data coming from some legacy system)and I need a case insensitive search so I have to use regex with option i – user7045224 Oct 27 '16 at 03:51
  • @user7045224 like i said, this is not going to work with a regex. Atleast not with such a large pattern string. The most simple option is to store a lower/upper variant in your documents (easily done during save) and just use a find expression on it. If that's not implementable you can use a aggregation in which you lower/upper your value and match then. The main problem with the aggregation is that the match will not use a index.Or the last thing is to wait until the next release of mongoDB it will include caseinsensitive indexes. Hope it's clear. – HoefMeistert Oct 27 '16 at 05:24
0

Yes, mongoDB regex has character limit, originates from perl regex limit. Because "MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support."MongoDB v3.2

You can see the limitation is 32764 characters:MongoDB add assert of regular expression length

I recently met this issue, and the solution was to use $in query operator instead. $in does not have characters limit. This suits my problem, since it was exact match rather than pattern matching in such long input case.

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
iAutumn
  • 1
  • 2