Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB special characters

I have inserted a init file into MongoDB:

db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }})

And if I invoke this entry with db.User.find(), than I get the following:

{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "�sterreich" } }

The word with special characters "�sterreich is not correct.

Does anybody have any idea what I can do in mongodb in order to solve this problem?

like image 323
quma Avatar asked Jul 09 '15 19:07

quma


People also ask

How to search a string with special characters in MongoDB document?

To search a string with special characters in MongoDB document, you can use \. Here, we have special character $ in our string. Let us first implement the following query to create a collection with documents Following is the query to display all documents from a collection with the help of find () method This will produce the following output

How do you name a collection in MongoDB?

MongoDB Collection Naming . Guidelines. Use Camel-casing or lower case name. It is preferred to use a lower case to avoid any unwanted problems with the casing. Collection names should not begin with below special characters like $ an empty string (e.g. “” ) or ; contain the null character ; prefixed with System. (As it reserved).

Can MongoDB field names contain dots and dollar signs?

Note: MongoDB 3.6 onwards the server permits the storage of field names that contain dots (i.e. . ) and dollar signs (i.e. $). One can avoid using any separator for the field names.

What characters are not allowed in a database name?

Database names must have fewer than 64 characters. Database names should contain alphanumeric characters. Below special symbols are not allowed on Windows OS. Symbols not allowed in Database names : / \ . ” $ * < > : | ?


2 Answers

JSON and BSON can only encode / decode valid UTF-8 strings, if your data (included input) is not UTF-8 you need to convert it before passing it to any JSON dependent system, like this:

$string = iconv('UTF-8', 'UTF-8//IGNORE', $string); // or
$string = iconv('UTF-8', 'UTF-8//TRANSLIT', $string); // or even
$string = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $string); // not sure how this behaves

Personally I prefer the first option, see the iconv() manual page. Other alternatives include:

  • mb_convert_encoding()

mb_convert_encoding("Österreich", "UTF-8", "ISO-8859-1");

  • utf8_encode(utf8_decode($string))

You should always make sure your strings are UTF-8 encoded, even the user-submitted one.

like image 59
Somnath Muluk Avatar answered Nov 16 '22 03:11

Somnath Muluk


Guess so you can use the HTML Codes inside a string

Code:

You can use &ouml ; to save the spl char in db.

db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "&ouml;sterreich" }})

And on invoking this entry with db.User.find(),you will get the following:

{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" } }

Reference:

http://www.starr.net/is/type/htmlcodes.html

Replace multiple characters in a string in javascript

Hope this helps.

like image 44
SUNDARRAJAN K Avatar answered Nov 16 '22 03:11

SUNDARRAJAN K