Any reason why you want the backslash? In principle you use the backslash in your code because you want the quotes. I am not sure in python but in some other languages you put 3 backslashes. The first one escapes the second one and the third escape the quotes.
I do need the backslashes. Actually, the documents I want to retrieve are much more complicated than
the simple example I gave, and they are in a database that I cannot modify.
The documents are correctly retrieved by a Java application, but for some reason pymongo “unescapes” the backslashes.
Ok, so I’ve found what the problem was. It’s the “print” bit, that is not displaying the backslashes. My code was actually doing a return str(doc) as it was a REST API server. The str was then removing the backslashes.
I’ve changed that for return json.dumps(doc) and it keeps them.