Mongoimport重复键

时间:2018-04-05 18:00:25

标签: mongodb mongoimport

当我使用mongoimport将json文件导入我的集合时,只会导入同名的第一个对象。

原文abc.json

{
   "timestamp":"100",
   "layers":{
      "a":{
         "aa":"0",
         "ab":"1",
         "ac":"2"
      },
      "b":
         {
          "ba":"0",
          "bb":"1",
          "bc":"2"
         },
       "b":
         {
            "missing": "1",
            "missing2": "2"
         }

   }
}

导入命令如下所示:

mongoimport --db myDB --collection myCollection --file abc.json

db.myCollection.find()。limit(1)的输出如下所示:

{
   "_id":ObjectId("5ac723b5d1b034017a6f187f"),
   "timestamp":"100",
   "layers":{
      "a":{
         "aa":"0",
         "ab":"1",
         "ac":"2"
      },
      "b":{
         "ba":"0",
         "bb":"1",
         "bc":"2"
      }
   }
}

是否可以将此重复键与mongoimport合并?

1 个答案:

答案 0 :(得分:0)

Mongo导入将导入与有效json文件中完全相同的文档。

示例json文件abc.json

{
    "timestamp": "100",
    "layers": {
        "a": {
            "aa": "0",
            "ab": "1",
            "ac": "2"
        },
        "b": {
            "ba": "0",
            "bb": "1",
            "bc": "2"
        }
    }
}
{
    "timestamp": "100",
    "layers": {
        "a": {
            "aa": "0",
            "ab": "1",
            "ac": "2"
        },
        "b": [{
                "ba": "0",
                "bb": "1",
                "bc": "2",
                "baa": [
                    "1",
                    "2",
                    "3"
                ],
                "bba": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "missing": "1",
                "missing2": "2"
            }
        ]
    }
}

来自控制台的mongodb import命令

mongoimport --db myDB --collection myCollection --file abc.json

导入日志

2018-04-05T23:55:16.500+0530    connected to: localhost
2018-04-05T23:55:17.727+0530    imported 2 documents

查找

> db.myCollection.find().pretty()

控制台视图

{
        "_id" : ObjectId("5ac6678b597b04c8add377d9"),
        "timestamp" : "100",
        "layers" : {
                "a" : {
                        "aa" : "0",
                        "ab" : "1",
                        "ac" : "2"
                },
                "b" : {
                        "ba" : "0",
                        "bb" : "1",
                        "bc" : "2"
                }
        }
}
{
        "_id" : ObjectId("5ac6678b597b04c8add377da"),
        "timestamp" : "100",
        "layers" : {
                "a" : {
                        "aa" : "0",
                        "ab" : "1",
                        "ac" : "2"
                },
                "b" : [
                        {
                                "ba" : "0",
                                "bb" : "1",
                                "bc" : "2",
                                "baa" : [
                                        "1",
                                        "2",
                                        "3"
                                ],
                                "bba" : [
                                        "1",
                                        "2",
                                        "3"
                                ]
                        },
                        {
                                "missing" : "1",
                                "missing2" : "2"
                        }
                ]
        }
}