MongoDB特殊字符

MongoDB特殊字符,mongodb,spring-data,special-characters,string-formatting,utf,Mongodb,Spring Data,Special Characters,String Formatting,Utf,我在MongoDB中插入了一个init文件: db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }}) 如果使用db.User.find()调用此条目,则会

我在MongoDB中插入了一个init文件:

db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }})
如果使用db.User.find()调用此条目,则会得到以下结果:

{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "�sterreich" } }
带有特殊字符“èè½sterreich”的单词不正确


有人知道我可以在mongodb中做些什么来解决这个问题吗?

猜猜看,这样就可以在字符串中使用HTML代码了

代码

您可以使用ö;将spl字符保存在db中

db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "österreich" }})
使用db.User.find()调用此条目时,您将得到以下结果:

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


希望这能有所帮助。

JSON和BSON只能对有效的UTF-8字符串进行编码/解码,如果您的数据(包括输入)不是UTF-8,则在将其传递给任何依赖JSON的系统之前,需要对其进行转换,如下所示:

$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
就我个人而言,我更喜欢第一种选择,参见手册页。其他选择包括:

mb_转换_编码(“Österreich”、“UTF-8”、“ISO-8859-1”)

  • utf8\u编码(utf8\u解码($string))

你应该始终确保你的字符串是UTF-8编码的,即使是用户提交的。

你使用的是什么版本的mongodb?你是从mongo控制台得到这个结果的吗?我也使用v3.0.7。我尝试了你的代码,没有任何奇怪的情况。我想问Rabee的相同问题;”您是否从mongo控制台获得此结果?“。因为MongoDB以BSON格式将数据存储为UTF8编码。更改可能发生在发送到MongoDB之前。祝你好运。你安装Linux语言包了吗?尤其是您的字符串所在的语言??您正在将结果打印到的控制台,正试图在
UTF-16
或其他一些多字节字符集中表示单字节字符(
UTF-8
)。您需要更改控制台设置以
UTF-8
格式显示字符。