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?
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
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).
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.
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 : / \ . ” $ * < > : | ?
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.
Guess so you can use the HTML Codes inside a string
Code:
You can use ö ; 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" : "ö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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With