使用mongoimport将json从文件导入到mongodb [英] Importing json from file into mongodb using mongoimport

查看:95
本文介绍了使用mongoimport将json从文件导入到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的json_file.json:

I have my json_file.json like this:

[
{
    "project": "project_1",
    "coord1": 2,
    "coord2": 10,
    "status": "yes",
    "priority": 7
},
{
    "project": "project_2",
    "coord1": 2,
    "coord2": 10,
    "status": "yes",
    "priority": 7
},
{
    "project": "project_3",
    "coord1": 2,
    "coord2": 10,
    "status": "yes",
    "priority": 7
}
]

当我运行以下命令以将其导入到mongodb中时:

When I run the following command to import this into mongodb:

mongoimport --db my_db --collection my_collection --file json_file.json 

我收到以下错误:

Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?

如果我将--jsonArray标志添加到我这样导入的命令中:

If I add the --jsonArray flag to the command I import like this:

imported 3 documents

而不是原始文件中所示的json格式的一个文档.

instead of one document with the json format as shown in the original file.

如何使用上面显示的文件中的原始格式将json导入到mongodb中?

How can I import json into mongodb with the original format in the file shown above?

推荐答案

也许以下来自MongoDB项目博客的参考可以帮助您深入了解数组在Mongo中的工作方式:

Perhaps the following reference from the MongoDB project blog could help you gain insight on how arrays work in Mongo:

https://blog.mlab.com/2013 /04/thinking-about-arrays-in-mongodb/

否则,我将为您构建导入框架,或者:

I would frame your import otherwise, and either:

a)使用--jsonArray标志将三个不同的对象分别导入到集合中;或

a) import the three different objects separately into the collection as you say, using the --jsonArray flag; or

b)将完整的数组封装在单个对象中,例如,以这种方式:

b) encapsulate the complete array within a single object, for example in this way:

{
"mydata": 
    [
    {
          "project": "project_1",
          ...
          "priority": 7
    }
    ]
}

HTH.

这篇关于使用mongoimport将json从文件导入到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆