0

我已经安装了 mongodb 2.4.5 版本并且可以在 linux CENTOS 上正常工作,但是当我尝试插入特殊字符时出现问题,下面是一个示例。

使用 PHP 我做了以下...

$con = new MongoClient(); //connect

$db = $con->selectDB('test');
$dbs = $db->titles; //Set to titles collection
$dbs->insert(array('name' => 'televisión')); //Inserts the item into the titles collection

但是在mongodb数据库服务器中保存如下

> db.titles.find()
{ "name" : "televisi��n" }

我想知道是否有任何方法可以将带有特殊字符的数据存储到数据库中,即{ "name" : "televisión" }

提前致谢

4

2 回答 2

1

Based on mb_detect_encoding(), "televisión" appears to be a UTF-8 string, so you should not have any problem storing it in MongoDB. Indeed, I was able to insert it as both a key and a value into the database, from both PHP and the JavaScript shell.

Are you able to insert the document and retrieve it using PHP? Your example only showed the insertion step. Consider the following script:

$name = 'televisión';

$m = new MongoClient();
$c = $m->test->titles;
$c->insert(['name' => $name]);
var_dump($c->findOne(['name' => $name]));

This script prints the following on my system:

array(2) {
  '_id' =>
  class MongoId#6 (1) {
    public $$id =>
    string(24) "5213c3dbe84df10121873458"
  }
  'name' =>
  string(11) "televisión"
}

Additionally, I'm able to query for the same document in the JavaScript shell:

> db.titles.find({name: "televisión"})
{ "_id" : ObjectId("5213c3dbe84df10121873458"), "name" : "televisión" }

It's possible that your terminal is simply having trouble displaying UTF-8 characters.

于 2013-08-20T19:36:14.800 回答
0

我刚刚遇到了拉丁字符(çãóõé...)的相同问题,但使用的是Node.js。这是我解决这个问题的方法:

JavascriptString.toString("utf8");
于 2015-03-06T17:12:39.823 回答