[英]node.js cannot read json from mongoexport
I've exported a collection from mongo using the following command: 我已经使用以下命令从mongo中导出了一个集合:
mongoexport -d <database-name> -c <collection-name> -o foo.json
Export worked fine and produced foo.json
which looked like this: 导出工作正常,并生成了foo.json
:
{
"_id" : { "$oid" : "76safuysadf76tsaydgf" },
"name" : "John",
"number" : 3
}
{
"_id" : { "$oid" : "dfsafuysaasdf7tsayd6" },
"name" : "Fred",
"number" : 4
}
{
"_id" : { "$oid" : "876sfuyg7rfasff4ffff" },
"name" : "Paul",
"number" : 1
}
Now I want to read this json file with node.js, edit the data and save back to the file (or a new file). 现在,我想使用node.js读取此json文件,编辑数据并保存回该文件(或新文件)。 For example I want to remove the number
field from each user. 例如,我想从每个用户中删除number
字段。
I'm having trouble reading the file with node. 我在使用node读取文件时遇到问题。 I tried this: 我尝试了这个:
var fs = require("fs");
var obj = JSON.parse(fs.readFileSync("./foo.json"));
console.log(obj);
But I get the following error: 但是我收到以下错误:
uncaughtException: Unexpected token {
SyntaxError: Unexpected token {
EDIT: From the mongoexport
docs: http://docs.mongodb.org/manual/reference/program/mongoexport/ 编辑:从mongoexport
文档: http : //docs.mongodb.org/manual/reference/program/mongoexport/
Use the --jsonArray
option to get output in a JSON array, as I described in my original answer. 如我在原始答案中所述,使用--jsonArray
选项可获取JSON数组中的输出。
Original Answer 原始答案
It looks like you are trying to parse a JSON array, instead of one object. 似乎您正在尝试解析JSON数组,而不是一个对象。 Try adding []
and commas to the output from mongoexport so it looks like this: 尝试将[]
和逗号添加到mongoexport的输出中,如下所示:
[{
"_id" : { "$oid" : "76safuysadf76tsaydgf" },
"name" : "John",
"number" : 3
},
{
"_id" : { "$oid" : "dfsafuysaasdf7tsayd6" },
"name" : "Fred",
"number" : 4
},
{
"_id" : { "$oid" : "876sfuyg7rfasff4ffff" },
"name" : "Paul",
"number" : 1
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.