MongoDB“错误:检测到非 ASCII 字符”在 Node.js 上
我正在尝试将非常大的数据块解析到新的 MongoDB 数据库中,当我运行该作业时,我在执行 db.myCollection.find()
时不断收到错误。
错误为错误:检测到非 ascii 字符
。
我正在使用 Node.js 和 Mongoose 来填充数据库。我见过的唯一可能导致问题的字符是 '
、、
和 -
,但我已经对所有这些字符进行了 .replace。我尝试转义它们(\
),并且尝试完全删除它们,但似乎没有任何效果。
我最大的问题是,当我保存对象时,我没有收到任何错误,我只是在尝试查看集合时收到该消息。
有没有关于清理 MongoDB 输入的信息?
I'm trying to parse a very large chunk of data into a new MongoDB database and when I run the job I keep getting an error when I do db.myCollection.find()
.
The error is error: non ascii character detected
.
I am using Node.js and Mongoose to populate the database. The only characters I have seen that might be causing problems are '
, ,
and -
but I have done a .replace on all of them. I tried escaping them (\
) and I tried just removing them entirely and nothing seems to be working.
My biggest problem is that I don't get any errors when I am saving the objects, I just get that message when I try to view the collection.
Is there any information out there on sanitizing MongoDB input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它是字符串数据,那么在插入之前可以确保它被编码为 UTF8。大多数驱动程序应该检查这一点,但它可能在 Mongoose 驱动程序中被忽略。 MongoDB 中的所有字符串都存储为 UTF8,如 BSON 规范所定义。
If it's string data then maybe make sure it's encoded as UTF8 before inserting. Most drivers should check this, but it could've been overlooked in the Mongoose driver. All strings in MongoDB are stored as UTF8, as defined by the BSON spec.
刚刚有同样的问题。 MongoDB 版本:1.8.2 + Mongoose 3.3.1 (NodeJS v0.8.14) on Ubuntu (EC2)
共识是升级到最新版本的 MongoDB (2.2) 将解决该问题。
通过发出以下命令检查我正在运行的 MongoDB 版本:
确认 mongo 当前是否正在运行:
到 关闭 MongoDB
然后按照以下说明进行升级: 如何在 Ubuntu 上安装 MongoDB
将第 10 Gen(MongoDB 创建者)公钥添加到 apt-get,以便您信任他们的软件包:
更新您的软件包:
安装 10gen 的 MongoDB Debian/Ubuntu 软件包:
如果出现错误,请尝试:
确认 mongodb 用户/组有权写入数据目录:
使用以下命令重新启动 MongoDB:
一旦你有了新版本的 MongoDB,“非 ascii”字符问题就应该消失了。 :-)
Just had the same issue. MongoDB version: 1.8.2 + Mongoose 3.3.1 (NodeJS v0.8.14) on Ubuntu (EC2)
Consensus is that upgrading to the latest version of MongoDB (2.2) will solve the issue.
Checked which version of MongoDB I was running by issuing the command:
Confirm if mongo is currently running:
To Shutdown MongoDB
Then upgraded following these instructions: How to Install MongoDB on Ubuntu
Add the 10 Gen (creators of MongoDB) public key to apt-get so you trust their package:
Update your packages:
install 10gen's MongoDB Debian/Ubuntu package:
If you get an error, try:
Confirm that the mongodb user/group has permission to write to the data directory:
Restart MongoDB with the command:
Once you have the new version of MongoDB the "non ascii" character issue should be gone. :-)
时,我得到了它
当我不小心做了这个BasicDBObject bdb ;
bdb.put("myfield", new ObjectId(String.valueOf(objectId)));
然后将bdb保存到mongo。
i got it when i accidentally did this
BasicDBObject bdb;
bdb.put("myfield", new ObjectId(String.valueOf(objectId)));
then saved bdb to mongo.