mongodb 如何让mongo输入json文件而不出错?

8zzbczxx  于 2023-01-25  发布在  Go
关注(0)|答案(2)|浏览(123)

我有下面的json数据,我试图导入到mongodb。

{    
"subjectId": "63cd96779e66d518f3af574c",
"name":"Earth and Space Science",  
"subtopics": [
  {
    "name": "Rocks, Soil and Minerals",
    "questions": [
      {
        "question": "What type of rock is formed when magma cools and hardens?",
        "multipleChoice":["Sedimentary", "Metamorphic", "Igneous",  "All of the above"],
        "answer": "Igneous"
      }
    ]
  }
]
}

我收到错误:传入的操作不能是数组。
你知道为什么吗?

7jmck4yq

7jmck4yq1#

也许,问题是格式化的json结构与您的制表符和空格,它必须在Oneliner中取消格式化。
试试这个:

{"subjectId":"63cd96779e66d518f3af574c","name":"Earth and Space Science","subtopics":[{"name":"Rocks, Soil and Minerals","questions":[{"question":"What type of rock is formed when magma cools and hardens?","multipleChoice":["Sedimentary","Metamorphic","Igneous","All of the above"],"answer":"Igneous"}]}]}

参见this

gfttwv5a

gfttwv5a2#

MongoDB Compass有一个不同寻常的JSON文件导入格式要求,即每个文档必须用换行符分隔。
从JSON文件导入数据时,可以将数据格式化为:

  • 以换行符分隔的文档,或
  • 数组中逗号分隔的文档

按照要求格式化JSON文档文件后,MongoDBCompass会正常导入文件,希望Compass的文件导入解析器变得“更聪明”。

相关问题