title: Java: Error while inserting json array into MongoDB
body: <p>I am working on inserting a CSV file into MongoDB. First I am converting my CSV into Json formatted array (Reference: <a href="https://dzone.com/articles/how-to-convert-csv-to-json-in-java" rel="nofollow noreferrer">https://dzone.com/articles/how-to-convert-csv-to-json-in-java</a>) and then trying to upload it into MongoDB, but facing the below error(<strong>readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.</strong>):</p>
  
  <pre><code>Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
      at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:692)
      at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:724)
      at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:452)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:148)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
      at org.bson.Document.parse(Document.java:105)
      at org.bson.Document.parse(Document.java:90)
      at com.ucm.json.ConnectMongoDB.connectToMongoDB(ConnectMongoDB.java:52)
      at com.ucm.json.Main.main(Main.java:15)
  </code></pre>
  
  <p>My JSON string(result) looks like below:</p>
  
  <pre><code>[ {
    "query" : "ecn",
    "type" : "KeywordMatch",
    "url" : "http://insidedell.com/ecn",
    "description" : "ECN"
  }, {
    "query" : "product marketing",
    "type" : "PhraseMatch",
    "url" : "http://dellemc.com/product",
    "description" : "Products"
  }, {
    "query" : "jive",
    "type" : "ExactMatch",
    "url" : "http://test.com/jive",
    "description" : "Jive test"
  } ]
  </code></pre>
  
  <p>Below is my code:
  <strong>Step 1</strong>: Convert CSV into JSON format string array</p>
  
  <pre><code>public class CreateJSON {
  
      public String query;
      public String type;
      public String url;
      public String description; 
      String result ;
      public CreateJSON() {
  
      }
      public CreateJSON(String query,String type,String url,String description) {
          this.query = query;
          this.type = type;
          this.url = url;
          this.description = description;
  
      }
  
      public String createJsonFromCSV() throws IOException{
          String csvFile = "C:\\Projects\\frontKeymatch_default_frontend.csv";
  
          List&lt;CreateJSON&gt; createObjects = null;
          Pattern pattern = Pattern.compile(",");
          try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
  
              createObjects = in.lines().map(line -&gt;{
                  String[] x = pattern.split(line);
                  return new CreateJSON(x[0],x[1],x[2],x[3]);
  
              }).collect(Collectors.toList());
  
  
               ObjectMapper mapper = new ObjectMapper();
               mapper.enable(SerializationFeature.INDENT_OUTPUT);
               result =  mapper.writeValueAsString(createObjects);
  
          } 
           return result;
  
      }
  }
  </code></pre>
  
  <p><strong>Step 2)</strong> Connect to MongoDB and insert the data</p>
  
  <pre><code>public class ConnectMongoDB{
      public void connectToMongoDB(String resultFromCSV) throws JsonGenerationException, JsonMappingException, IOException {
  
      MongoClient mongo = new MongoClient( "localhost" ,27017);
      Document doc = Document.parse(resultFromCSV);
  
              mongo.getDatabase("db").getCollection("collection").insertOne(doc);
  
              System.out.println("success");
  
          }
     }    
  </code></pre>
  
  <p><strong>Step 3:</strong> My main method:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) throws FileNotFoundException, IOException {
  
          CreateJSON upload = new CreateJSON();
          ConnectMongoDB mongo = new ConnectMongoDB();
          mongo.connectToMongoDB(upload.createJsonFromCSV());
      }
  
  }
  </code></pre>
  
  <p>Any help is appreciated. Thanks</p>