...
Using extended $options for $regex (i.e. "x" or "s") does not work with explain output. Instead, we get the following error: Error: 16863 Error converting /a/msix in field 0 to a JS RegExp object: SyntaxError: Invalid flags supplied to RegExp constructor 'sx' at src/mongo/shell/types.js:612 It looks like there is an attempt at converting the $regex:... , $options:... format of a mongo regular expression into the javascript /.../ regular expression format when formatting the explain output, but the non-standard flags can't be appended to this.
asya commented on Wed, 25 Apr 2018 22:26:31 +0000: Seems fixed as of 3.5.1, closing. asya commented on Mon, 20 Mar 2017 19:40:54 +0000: This is still broken in 3.4 but appears to be fixed in 3.5.1 amar.hamzeh commented on Wed, 22 Feb 2017 02:22:38 +0000: I think this can happen anywhere the shell attempts to deserialise BSON Regex elements. For example, the same issue happens when querying system.profile for operations that used a regex with flag "s" Reproduction Steps: 1. Enable profiling db.setProfilingLevel(1, -1) 2. Query any collection (e.g. test.a) with > db.a.find({a: {$regex : "test", "$options" : "s"}}) > 3. Query the system.profile collection for that find operation, e.g. > db.system.profile.find({ns : "test.a"}) Error: SyntaxError: invalid regular expression flag s > If we query the same thing with Python, it works: >>> from pymongo import MongoClient >>> MongoClient('A-Mac.local:27017').test.system.profile.find_one({"ns" : "test.a"}) {u'cursorExhausted': True, u'writeConflicts': 0, u'docsExamined': 1, u'keysExamined': 0, u'protocol': u'op_command', u'execStats': {u'needYield': 0, u'direction': u'forward', u'saveState': 0, u'restoreState': 0, u'isEOF': 1, u'docsExamined': 1, u'nReturned': 0, u'needTime': 2, u'filter': {u'a': Regex(u'test', 16)}, u'executionTimeMillisEstimate': 0, u'invalidates': 0, u'works': 3, u'advanced': 0, u'stage': u'COLLSCAN'}, u'responseLength': 97, u'millis': 0, u'ts': datetime.datetime(2017, 2, 22, 1, 56, 43, 529000), u'nreturned': 0, u'client': u'127.0.0.1', u'locks': {u'Global': {u'acquireCount': {u'r': 2L}}, u'Collection': {u'acquireCount': {u'r': 1L}}, u'Database': {u'acquireCount': {u'r': 1L}}}, u'numYield': 0, u'user': u'', u'keyUpdates': 0, u'query': {u'filter': {u'a': {u'$options': u's', u'$regex': u'test'}}, u'find': u'a'}, u'ns': u'test.a', u'allUsers': [], u'op': u'query'} >>> The field execStats.filter.a is where BSON Regex type is returned by the server. I have tested this with 3.2.11 and 3.4.1 tyler@10gen.com commented on Mon, 25 Nov 2013 16:14:18 +0000: Hey Luke, This happens because some options that we support in the server are not actually supported by the RegExps in JavaScript. Specifically, the JS RegExp supports "i" and "m" but not the "s" or "x" options. To fix this problem we would need to create a custom FunctionTemplate (class) for these types of objects in the shell. Unfortunately, this would be backward breaking as the RegExp objects would no longer be usable directly without delegating the RegExp functionality. This warrants additional discussion about how to address the issue. dan@10gen.com commented on Tue, 19 Nov 2013 03:47:32 +0000: Against a 2.4.8 shell this actually crashes the shell, so 2.5 behavior is actually an improvement.... 2.4-mongo[v2.4]/$ ./mongo MongoDB shell version: 2.4.4-pre- connecting to: test > db.c.find({a:{$regex:"a", $options:"msix"}}).explain() Mon Nov 18 22:45:26.115 mongo got signal 11 (Segmentation fault: 11), stack trace: Mon Nov 18 22:45:26.121 0x10dd8126b 0x10dc9687f 0x7fff8ee61cfa 0x10ecb2148 0x10de216bb 0x10dd4bdc7 0x10dd4bdb9 0x10dd4d304 0x10df8232c 0x10df81d92 0x10dfc8082 0xe9c60406362 0xe9c6041701e 0 mongo 0x000000010dd8126b _ZN5mongo15printStackTraceERSo + 43 1 mongo 0x000000010dc9687f _Z12quitAbruptlyi + 191 2 libsystem_c.dylib 0x00007fff8ee61cfa _sigtramp + 26 3 ??? 0x000000010ecb2148 0x0 + 4543160648 4 mongo 0x000000010de216bb _ZN2v86Object3SetEjNS_6HandleINS_5ValueEEE + 283 5 mongo 0x000000010dd4bdc7 _ZN5mongo7V8Scope16mongoToV8ElementERKNS_11BSONElementEb + 791 6 mongo 0x000000010dd4bdb9 _ZN5mongo7V8Scope16mongoToV8ElementERKNS_11BSONElementEb + 777 7 mongo 0x000000010dd4d304 _ZN5mongoL8namedGetEN2v85LocalINS0_6StringEEERKNS0_12AccessorInfoE + 660 8 mongo 0x000000010df8232c _ZN2v88internal8JSObject35GetPropertyAttributeWithInterceptorEPS1_PNS0_6StringEb + 822 9 mongo 0x000000010df81d92 _ZN2v88internal10JSReceiver32GetPropertyAttributeWithReceiverEPS1_PNS0_6StringE + 252 10 mongo 0x000000010dfc8082 _ZN2v88internal19Runtime_HasPropertyENS0_9ArgumentsEPNS0_7IsolateE + 114 11 ??? 0x00000e9c60406362 0x0 + 16064792519522 12 ??? 0x00000e9c6041701e 0x0 + 16064792588318 luke.lovett commented on Tue, 19 Nov 2013 01:31:34 +0000: I can repro this on the 2.5.3 shell connecting to the 2.5.4 version of mongod. dan@10gen.com commented on Tue, 19 Nov 2013 01:25:30 +0000: i believe this is set in the v8 code here: src/third_party/v8/src/jsregexp.cc. is this a regression from 2.4 though?
> db.c.insert({a:"asdf"}) > db.c.find({a:{$regex:"a", $options:"msix"}}) { "_id" : ObjectId("528a91ff76468672f5307242"), "a" : "asdf" } > db.c.ensureIndex({a:1}) > db.c.find({a:{$regex:"a", $options:"msix"}}).explain() Mon Nov 18 22:20:46.260 Error: 16863 Error converting /a/msix in field 0 to a JS RegExp object: SyntaxError: Invalid flags supplied to RegExp constructor 'msix' at src/mongo/shell/types.js:612 > db.c.find({a:{$regex:"a", $options:"mi"}}).explain() { "cursor" : "BtreeCursor a_1 multi", "isMultiKey" : false, "n" : 2, "nscannedObjects" : 2, "nscanned" : 2, "nscannedObjectsAllPlans" : 2, "nscannedAllPlans" : 2, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "a" : [ [ "", { } ], [ /a/im, // js regexes can't handle 's' or 'x' here /a/im ] ] }, "server" : "llmac:27017" }