我有一个大小适中的json文件,需要导入到mongo中作为测试数据。但是,当我尝试使用mongoimport时,我会收到响应。
失败:错误处理文档#1:无效字符“}”查找对象键字符串的开头
下面是我试图导入的JSON的一部分。请注意,删除空行并没有为我解决问题,在这里找到的一些建议,比如通过linter运行JSON,并没有解决我的问题。它被确认为有效的JSON,但不适用于mongoimport。
[{
"prompt_id": "id1",
"prompt_text": "What is the best advice you’ve ever received? What were you doing at the time?"
},
{
"prompt_id": "id2",
"prompt_text": "Write from a ladybug’s point of view. What does a typical day look like for you?"
}]下面是我使用的终端命令
mongoimport --db testdb --collection prompts --file "fullfilepath\prompts.json"我的问题是“mongoimport如何看待JSON?”我猜它需要一个包装件(称为对象键字符串),但我不知道它应该是什么样子。
发布于 2017-10-04 21:39:13
我在数据库中的一个集合上使用mongoexport找到了答案,然后在JSON中迭代一个对象,直到它mongoimportd正确为止。之后,它只是几个简单的替换操作,从而使整个JSON导入正确。
因此,在回答我最初的问题时,mongoimport 需要一个对象id (下面的示例中是**_id**)才能使命令工作。
以下是JSON被接受时的样子:
{
"_id":{
"$oid": "69d4023e2cd5ac10f8b92d40"
},
"prompt_id": "promptid1",
"prompt_text": "What is the best advice you’ve ever received? What were you doing at the time?"
}
{
"_id":{
"$oid": "69d4023e2cd5ac10f8b92d41"
},
"prompt_id": "promptid2",
"prompt_text": "Write from a ladybug’s point of view. What does a typical day look like for you?""
}还请注意,我使用了相同的命令,而不需要使用--jsonArray标志。
mongoimport --db testdb --collection prompts --file "fullfilepath\prompts.json"https://stackoverflow.com/questions/46554330
复制相似问题