[SERVER-20451] Backslashes disappear in the shell Created: 17/Sep/15  Updated: 13/Mar/17  Resolved: 17/Sep/15

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.1.8
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Henrik Ingo (Inactive) Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to COMPASS-896 Document how Compass handles special ... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

db.validTemp.insert( { firstname : "Matti", lastname : "Meikäläinen", socnr : '120479-123B' } )
db.validTemp.find( { socnr : { "$regex" : '\d{6}[Aa-]\d{3}[0-9a-zA-Z]' } } )
db.validTemp.find( { socnr : { "$regex" : '\\d{6}[Aa-]\\d{3}[0-9a-zA-Z]' } } )
{ "_id" : ObjectId("55fa9f1ba85ead2d9e3e9c1a"), "firstname" : "Matti", "lastname" : "Meikäläinen", "socnr" : "120479-123B" }

With document validation:

db.runCommand( { collMod : "valid", 
                 validator : {   socnr  : { $regex : '\d{6}[Aa-]\d{3}[0-9a-zA-Z]' } } 
               } )
db.getCollectionInfos( { name : "valid" } )
db.runCommand( { collMod : "valid", 
                 validator : {   socnr  : { $regex : '\\d{6}[Aa-]\\d{3}[0-9a-zA-Z]' } } 
               } )
db.getCollectionInfos( { name : "valid" } )

Participants:

 Description   

Backslashes that are part of a $regex disappear before they get to the PCRE engine. You need to double-backslash to get a single backslash. Note that this happens also when the expression is within single quotes.

Same happens when using $regex for document validation. A nice feature here is that you can observe the disappearance with db.getCollectionInfos().



 Comments   
Comment by Henrik Ingo (Inactive) [ 17/Sep/15 ]

Ah sorry, I didn't realize that in JavaScript backslash has special meaning also with single quote strings. (Same happens in browser too.) I'll close this.

Generated at Thu Feb 08 03:54:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.