简体   繁体   English

mongo regex查询是否具有字符数限制,如果regex搜索字符串超过该限制,则会引发错误

[英]Does mongo regex query have character limit, if the regex search string is more than that limit it throws error

I am seeing mongo regex query not returning result when the regex searched string is very big, instead its throwing error. 我看到正则表达式搜索的字符串很大时,mongo正则表达式查询未返回结果,而是其抛出错误。 I have a scenario where I append lot of names to do a regex and thus my regex search string goes beyond 40000 characters. 我有一种情况,我会追加很多名称来进行正则表达式,因此我的正则表达式搜索字符串会超过40000个字符。 eg: 例如:

db.getCollection('collection').find(
    {"name":{"$regex" :"name1 | name2 | name3", "$options":"-i"}}
)

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. 将在所有“ namex”值上匹配,其中x是十进制。

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 如果要匹配多个字符串值,可以使用$ and运算符

Yes, mongoDB regex has character limit, originates from perl regex limit. 是的,mongoDB regex有字符数限制,起源于perl regex限制。 Because "MongoDB uses Perl compatible regular expressions (ie “PCRE” ) version 8.41 with UTF-8 support." 因为“ MongoDB使用支持UTF-8的Perl兼容正则表达式(即“ PCRE”)版本8.41。” MongoDB v3.2 MongoDB v3.2

You can see the limitation is 32764 characters: MongoDB add assert of regular expression length 您可以看到限制为32764个字符: MongoDB添加了正则表达式长度的断言

I recently met this issue, and the solution was to use $in query operator instead. 我最近遇到了这个问题,解决方案是改用$ in查询运算符。 $in does not have characters limit. $ in没有字符数限制。 This suits my problem, since it was exact match rather than pattern matching in such long input case. 这很适合我的问题,因为在这么长的输入情况下,它是精确匹配而不是模式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM