Inserting a record with backslash is throwing exception from Robo3T

Hi,

While inserting records with backslash(""), I am getting “Unable to parse JSON: Expecting 4 hex digits, at (2,33) from Robo 3T -1.4 IDE”

Is it a limitation? If yes then any workaround for inserting record with a backslash

Thanks & Regards,
Saravanan A.G

Hi @Saravanan_Alagumalai_Ganesan and welcome to the MongoDB community forums.

The problem is not with 3T, but with Javascript and string interpretation. The backslash character (\) starts an escape sequence. Because of this, the MongoDB engine thinks you’re try to store the character \u which it is having problems with. What you want to do, if you’re trying to store a backslash in the value is to actually put two backslashes in a row as follows:

{
    "notesJsonErrorFilePath": "abc\\uvw"
}

This will store the correct value, although it might not look like it when you return the data with a find() call:

Storing escape sequences does have it’s advantages as you can store tabs (\t), new lines (\n), carriage returns (\r) and embed quotes (\") in a string.

Hopefully this helps you out.

Thanks @Doug_Duncan.

If I try to insert the below record
{
“notesJsonErrorFilePath” : “abc\cba”
}

then the value gets stored without backslash as below
{
“_id” : ObjectId(“62f63089c3620c7a68ab811d”),
“notesJsonErrorFilePath” : “abccba”
}

and the global search also works irrespective of backslash. Any suggestions

Search Query
db.getCollection(‘MyCollection’).find({ $text: { $search: ““ab\ccba”” } })
db.getCollection(‘MyCollection’).find({ $text: { $search: ““abc\cba”” } })
db.getCollection(‘MyCollection’).find({ $text: { $search: ““abccba”” } })

Result
/* 1 */
{
“_id” : ObjectId(“62f631c1c3620c7a68ab834c”),
“notesJsonErrorFilePath” : “abccba”
}

Thanks & Regards,
Saravanan A.G

Since \c isn’t a recognized escape sequence in Javascript, the \ is dropped and the c is stored as normal. This will not happen with all characters however. For example, \t will be stored as \t and you will have an embedded tab in the string. As I stated before, if your value contains a \ character, you will want to store the value with \\.

As for the \u character, this signifies that you’re typing a unicode character and expects a four digit hex value. The \x character signifies a two digit hex value. Sorry for not calling that out in my earlier post.

You can read more on javascript escape sequences if your interested.

1 Like