Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-20451

Backslashes disappear in the shell

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.1.8
    • Component/s: Querying
    • None
    • Fully Compatible
    • ALL
    • Hide
      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" } )
      
      Show
      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" } )

      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().

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            henrik.ingo@mongodb.com Henrik Ingo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: