MongoDB的Perl driver的中文乱码问题

Perl下面向mongodb插入中文字符串会出现乱码. 根据MongoDB的文档, MongoDB支持UTF-8的编码. 但在Perl中, 如果直接使用utf8的字符串,也会出现问题. 测试代码: my $mongo_dbh = $mongo_connection->get_database( $mongo_db ); my $t = $mongo_dbh->get_collection(’test’); my $word = ‘测试’; $t->insert({ title => $word }); my $row = $t->find_one(); say “title:”,$row->{title}; $t->remove(); 输出结果是乱码. 在mongo shell和PHP中得到的也是乱码. 我初步判断是perl driver没有能够识别utf8编码而是强制encode成utf8编码后存储. 修改如下: my $mongo_dbh = $mongo_connection->get_database( $mongo_db ); my $t = $mongo_dbh->get_collection(’test’); my $word = ‘测试’; $t->insert({ title => decode_utf8($word) }); my $row = $t->find_one(); say “title:”,$row->{title}; $t->remove(); 输出正常. 判断正确, 问题解决. 希望Kristina能够修改就无须多此一举(当然,如果是非utf8编码还是需要转换的), 也许并不是bug而是个feature? UPDATE: Kristina的回复很迅速, 一觉醒来, [...]

你可能感兴趣的:(mongodb,PHP,perl)