Can anyone tell me why this command doesn't work from 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 are the slashes going?
I've tried "double escaping" with \\D and that inserts both slashes:
"mask" : "\\D\\D\\D\\D\\D\\D\\D\\D",
MongoDB shell version: 2.0.6, MongoDB version: 2.0.5, OSX Lion
Thanks
Actually, MongoDB does properly store the backslashes in the database. What you're seeing is an artifact of how the mongo shell displays strings which contain the backslash character. It will print them as escape sequences rather than as single characters. If you look at the actual data that comes back, you'll see that it's 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's double-backslashes
> x.str;
\D\D\D\D\D // but it's really not
x.str.quote();
"\\D\\D\\D\\D\\D" // it's what String.quote() prints
use regex notation (without quotes)
/\D\D\D\D\D\D\D\D/
Or use four slashes.
"////D" ==> "/D"
This is a case of *nix and C creeping into everything. In C, the character \ is the escape character. If allows you to "escape" the next character or characters to form some special character or character sequence. Thus, \n is newline (0x0a) and \d is carriage return (0x0d). So, since \ has this special meaning, in order to get a \ you have to have two of them.
Change your string to "\D\D\D\D\D\D\D\D"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With