Real Dreams
Real Dreams

Reputation: 18010

MongoDB \uXXXX issue

In my collection following query returns some result:

db.c.find({t:/a/})

But below query (and any other regex that has \uXXXX) returns no result:

db.c.find({t:/\u0041/})

What is wrong?

Upvotes: 2

Views: 953

Answers (2)

Real Dreams
Real Dreams

Reputation: 18010

Using Ray Toal's answer, Using PCRE \x{XXXX} syntax instead of JavaScript \uXXXX solved problem.

db.c.find({t:/\x{0041}/})

Upvotes: 4

Ray Toal
Ray Toal

Reputation: 88378

The reason for this is that according to the documentation

MongoDB uses PCRE for regular expressions.

However the PCRE documentation says

The following Perl escape sequences are not supported: \l, \u, \L, \U, and \N when followed by a character name or Unicode value. (\N on its own, matching a non-newline character, is supported.) In fact these are implemented by Perl's general string-handling and are not part of its pattern matching engine. If any of these are encountered by PCRE, an error is generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, \U and \u are interpreted as JavaScript interprets them.

That said, this SO question may be of some help.

Upvotes: 2

Related Questions