title: Error while inserting an array of JSON documents in to MongoDB using Java
body: <p>I am trying to insert a json string which contains an array of documents but getting following exception.</p>
  
  <p><strong>MongoDB server version: <code>3.0.6</code></strong></p>
  
  <p><strong>Mongo-Java driver version: <code>3.1.0</code></strong></p>
  
  <p>I understand that <code>insertOne()</code> method is used to insert just one document but over here it's an array of documents. I am not sure how to use <code>insertMany()</code> method here.</p>
  
  <p>Please guide.</p>
  
  <p><strong>JSON String that I want to insert:</strong></p>
  
  <pre><code>json = [{"freightCompanyId":201,"name":"USPS","price":8.00},{"freightCompanyId":202,"name":"FedEx","price":10.00},{"freightCompanyId":203,"name":"UPS","price":12.00},{"freightCompanyId":204,"name":"Other","price":15.00}]
  </code></pre>
  
  <p><strong>Exception Log:</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:655)
      at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)
      at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:421)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:138)
      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.ebayenterprise.ecp.jobs.Main.insert(Main.java:52)
      at com.ebayenterprise.ecp.jobs.Main.main(Main.java:31)
  </code></pre>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>public class Main {
  
      private static final Logger LOG = Logger.getLogger(Main.class);
  
      public static void main(String[] args) throws IOException {
          String json = getAllFreightCompanies();
          insert(json);
      }
  
      private static String getAllFreightCompanies() throws IOException {
          FreightCompanyDao freightCompanyDao = new FreightCompanyDaoImpl(DataSourceFactory.getDataSource(DatabaseType.POSTGRES.name()));
          List&lt;FreightCompany&gt; freightCompanies = freightCompanyDao.getAllFreightCompanies();
          return GenericUtils.toJson(freightCompanies);
      }
  
      private static void insert(String json) {
          MongoClient mongoClient = new MongoClient("GSI-547576", 27017);
          MongoDatabase database = mongoClient.getDatabase("test");
          MongoCollection&lt;Document&gt; table = database.getCollection("fc");
          Document document = Document.parse(json);
          table.insertOne(document);
      }
  
  }
  </code></pre>
  
  <p>GenericUtils.java</p>
  
  <pre><code>public final class GenericUtils {
  
      private static final Logger LOG = Logger.getLogger(GenericUtils.class);
  
      private GenericUtils() {
      }
  
      public static String toJson(List&lt;FreightCompany&gt; freightCompanies) throws IOException {
          String json = new ObjectMapper().writer().writeValueAsString(freightCompanies);
          LOG.debug("json = " + json);
          return json;
      }
  
  }
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
      &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
      &lt;version&gt;3.1.0&lt;/version&gt;
      &lt;type&gt;jar&lt;/type&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
     &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
     &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
     &lt;version&gt;1.9.13&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>