mongodbimportnosqlmeanstudio3t

how to import data to mongodb with multiple index


I am quite new to mongodb, I just created a database name Test and inside have a collection called projects i have a json file which contain 2 projects like this :

{
  "-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"
  }
}

so I want to import this to the projects collection. I was running this command

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

But the thing is the result i got is the mongodb create only one index ( 1 id ) which contains the whole thing.

What i want is after importing , mongodb will create 2 different indexes ( 2 ids ) for each of the project above

This is the current result. I was using Studio 3T enter image description here

thanks


Solution

  • You seem to be missing one curly braces(to nest object itself)

    {
      {"-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"
     }}
    }
    

    UPDATE:

    By default, monogoimport assumes a special structure for a file to import from: similar to JSON format except that only one document per line is allowed with no comma after each of them

    {
      "-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"
      }
    }