Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to insert a string that contains double quotes and single quotes as below.

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

When I try insert above, it got error as my field contain double quotes, can some one tell me something like escape sequence to insert double quote?

Can't do as under my field also contain single quotes. db.collectionName.insert({"filedName" : 'my field contains "double quotes" and 'single quotes' how to insert'}) ;

like image 526
user3706888 Avatar asked Aug 25 '14 08:08

user3706888


People also ask

What is the escape sequence for a double quote?

To print quotes, using escape characters we have two options: For single quotes: \' (backslash followed by single quote) For double quotes: \” (backslash followed by double quotes)

How do you insert a double quote into a quoted string?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters.

How do you escape quotation marks in a string?

' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.


1 Answers

I think it depends on which context you use your code. If it's in pure js (node.js for example) you can escape the quote char with \, like this :

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

But in the HTML context it's not possible, you need to replace the double-quote with the proper XML entity representation, "

like image 193
BaptisteL Avatar answered Sep 24 '22 16:09

BaptisteL