Why doesn't MongoDb keep my slashes on this line?

Can someone tell me why this command does not work with the MongoDB shell client:

db.coll.update({'live':true},{$set:{'mask':"\D\D\D\D\D\D\D\D"}},false,true) 

but

 db.coll.findOne({'id':'someId'}) 

returns the mask field as:

 "mask" : "DDDDDDDD", 

Where do the slashes go?

I tried "double escaping" with \\ D and inserted both traits:

 "mask" : "\\D\\D\\D\\D\\D\\D\\D\\D", 

MongoDB Shell Version: 2.0.6, MongoDB Version: 2.0.5, OSX Lion

thanks

+4
source share
3 answers

use regex notation (without quotes)

 /\D\D\D\D\D\D\D\D/ 

Or use four slashes.

 "////D" ==> "/D" 
+1
source

Actually, MongoDB correctly stores backslashes in the database. What you see is an artifact of how the mongo shell displays strings that contain a backslash character. It will print them as escape sequences, and not as single characters. If you look at the actual data that will be returned, you will see that it is correct.

 > db.tst.drop() true > db.tst.insert({ _id: 1, str: "\\D\\D\\D\\D\\D"} ); > x = db.tst.findOne( {_id:1} ); { "_id" : 1, "str" : "\\D\\D\\D\\D\\D" } // looks like it double-backslashes > x.str; \D\D\D\D\D // but it really not x.str.quote(); "\\D\\D\\D\\D\\D" // it what String.quote() prints 
+11
source

This is the case when * nix and C creep into everything. In C, the \ character is an escape character. If you can "escape" from the next character or characters to form a special character or sequence of characters. Thus, \ n is the newline (0x0a), and \ d is the carriage return (0x0d). So, since \ has this special meaning, to get \, you must have two of them.

Change the line to "\ D \ D \ D \ D \ D \ D \ D \ D \ D"

+1
source

All Articles