1

我对 mongodb 很陌生,我刚刚创建了一个database名称Test,里面有一个collectionprojects 我有一个 json 文件,其中包含 2 个这样的项目:

{
  "-KutvlMPRHGWWTgk1Mh4" : {
    "isPrivate" : true,
    "proj_contact" : "blMufEZjv6ZmnugDlTruzGwmowI3",
    "proj_creator" : "blMufEZjv6ZmnugDlTruzGwmowI3",
    "proj_date" : "01/01/2017",
    "proj_des" : "this is a testing projectasfasfasgasg",
    "proj_name" : "Project",
    "proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
  },
  "-KvARDkcsCEKZgnM94xH" : {
    "isPrivate" : false,
    "proj_autho" : [ "Lei0ohIcMBbzVtcQD1AgU4wHBKR2", "OnjEOFSNVpf189J9hAaSFWOZdl42", "0DwfVUeMAraiRxFZTa6xyjn6sfx2", "WHjFSvEo45U7ULzwIQdnUz6ytXp1", "kKTWt36bIifrRzpqNP1l2Gnl3KU2" ],
    "proj_contact" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
    "proj_creator" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
    "proj_date" : "10/12/2017",
    "proj_des" : "this is a test :)",
    "proj_name" : "another test",
    "proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
  }
}

所以我想将它导入到projects collection. 我正在运行这个命令

mongoimport --db Test --collection projects --file import-file.json

但事情是我得到的结果是 mongodb 只创建一个包含整个内容的索引( 1 id ) 。

我想要的是在导入之后,mongodb 将为上面的每个项目创建2 个不同的索引(2 个 id)

这是目前的结果。我使用的是 Studio 3T在此处输入图像描述

谢谢

4

1 回答 1

1

您似乎缺少一个花括号(嵌套对象本身)

{
  {"-KutvlMPRHGWWTgk1Mh4" : {
"isPrivate" : true,
"proj_contact" : "blMufEZjv6ZmnugDlTruzGwmowI3",
"proj_creator" : "blMufEZjv6ZmnugDlTruzGwmowI3",
"proj_date" : "01/01/2017",
"proj_des" : "this is a testing projectasfasfasgasg",
"proj_name" : "Project",
"proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
}},
{"-KvARDkcsCEKZgnM94xH" : {
"isPrivate" : false,
"proj_autho" : [ "Lei0ohIcMBbzVtcQD1AgU4wHBKR2", "OnjEOFSNVpf189J9hAaSFWOZdl42", "0DwfVUeMAraiRxFZTa6xyjn6sfx2", "WHjFSvEo45U7ULzwIQdnUz6ytXp1", "kKTWt36bIifrRzpqNP1l2Gnl3KU2" ],
"proj_contact" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
"proj_creator" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
"proj_date" : "10/12/2017",
"proj_des" : "this is a test :)",
"proj_name" : "another test",
"proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
 }}
}

更新:

默认情况下,monogoimport 为要导入的文件假定一个特殊的结构:类似于 JSON 格式,除了每行只允许一个文档,每个文档后面都没有逗号

{
  "-KutvlMPRHGWWTgk1Mh4" : {
    "isPrivate" : true,
    "proj_contact" : "blMufEZjv6ZmnugDlTruzGwmowI3",
    "proj_creator" : "blMufEZjv6ZmnugDlTruzGwmowI3",
    "proj_date" : "01/01/2017",
    "proj_des" : "this is a testing projectasfasfasgasg",
    "proj_name" : "Project",
    "proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
  }
  "-KvARDkcsCEKZgnM94xH" : {
    "isPrivate" : false,
    "proj_autho" : [ "Lei0ohIcMBbzVtcQD1AgU4wHBKR2", "OnjEOFSNVpf189J9hAaSFWOZdl42", "0DwfVUeMAraiRxFZTa6xyjn6sfx2", "WHjFSvEo45U7ULzwIQdnUz6ytXp1", "kKTWt36bIifrRzpqNP1l2Gnl3KU2" ],
    "proj_contact" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
    "proj_creator" : "kKTWt36bIifrRzpqNP1l2Gnl3KU2",
    "proj_date" : "10/12/2017",
    "proj_des" : "this is a test :)",
    "proj_name" : "another test",
    "proj_pi" : "qCQee2pWGeN9PD3FRXbJpdN7mHT2"
  }
}
于 2018-06-20T08:54:45.540 回答