Insert keywords (double quotes) in mongodb using the insert statement. (scape sequence)

want to insert a string containing double quotes and single quotes, as follows.

db.collectionName.insert ({"filedName": "my field contains" double quotes "and" single quotes ", how to insert"});

when I try to paste above, it got an error since my field contains double quotes, can someone tell me something like a scape sequence to insert a double quote?

I can’t do what in my field also contains single quotes. db.collectionName.insert ({"filedName": 'my field contains "double quotes" and "single quotes", how to insert'});

Thanks javad

+5
source share
1 answer

I think it depends on the context in which you use your code. If it is in pure js (e.g. node.js), you can avoid the char quote with \ , for example:

 db.collectionName.insert({"filedName" : "my field contains \"double quotes\" and 'single quotes' how to insert"}) ; 

But in the context of HTML, this is not possible, you need to replace the double quote with the correct representation of the XML entity, "

+3
source

Source: https://habr.com/ru/post/1201058/


All Articles