title: Import Excel Data to Mongodb using Java
body: <p>Tried Importing Excel Data to Mongo db in the Following Document Format</p>
  
  <pre><code>[
  {"productId":"",
  "programeName":"",
  "programeThumbImageURL":"",
  "programeURL":"",
  "programEditors":["editor1","editor2"],
  "programChapters":[
  {
  "chapterName":"chapter1",
  "authorNames":["authorName1","authorname2"]
  },
  {"chapterName":"chapter2"},
  "authorNames":["authorName1","authorName2"]
  }
  ,...
  ]},...]
  </code></pre>
  
  <p>There are many products in the Excel with with chapterNames has multiple authors. following is the code which tried executing and i could do inserting data. But the i couldn't merge the authorNames corresponding to a particular chapterName as above. So currently there are programChapters array contains objects as duplicated chapterNames. Following code shows my experiment towards this.</p>
  
  <pre><code>private static XSSFWorkbook myWorkBook;
  
  public static void main(String[] args) throws IOException {
  
  String[] programs = {"programName1","programName2","programName3","programName4",...};
  
  @SuppressWarnings("deprecation")
  Mongo mongo = new Mongo("localhost", 27017);
  @SuppressWarnings("deprecation")
  DB db = mongo.getDB("dbName");
  
  DBCollection collection = db.getCollection("programsCollection");
  
  File myFile =
      new File("dsm_article_author_details.xlsx");
  FileInputStream fis = new FileInputStream(myFile); // Finds the workbook instance for XLSX file
  myWorkBook = new XSSFWorkbook(fis);
  XSSFSheet mySheet = myWorkBook.getSheetAt(0); // Get iterator to all the rows in current sheet
  
  @SuppressWarnings("unused")
  Iterator&lt;Row&gt; rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
  for (String program : programs) {
    String programName = "";
    String chapterName = "";
    String authorName = "";
  
    BasicDBObject product = new BasicDBObject();
  
    BasicDBList programChaptersList = new BasicDBList();
    // For Each Row , Create Chapters Object here
  
    for (int i = 0; i &lt;= mySheet.getLastRowNum(); i++) { // points to the starting of excel i.e
                                                         // excel first row
      Row row = (Row) mySheet.getRow(i); // sheet number
      System.out.println("Row is :" + row.getRowNum());
  
      BasicDBObject programChapters = new BasicDBObject();
      if (row.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
        programName = row.getCell(0).getStringCellValue();
        System.out.println("programName : " + programName);
      }
  
      if (row.getCell(1).getCellType() == Cell.CELL_TYPE_STRING) {
        chapterName = row.getCell(1).getStringCellValue().replaceAll("\n", "");
        System.out.println("chapterName : " + chapterName);
      }
  
      if (row.getCell(2).getCellType() == Cell.CELL_TYPE_STRING) {
        authorName = row.getCell(2).getStringCellValue();
        System.out.println("authorName : " + authorName);
      }
  
      List&lt;String&gt; authors = new ArrayList&lt;String&gt;();
  
      programChapters.put("chapterName", chapterName);
  
      authors.add(authorName);
      programChapters.put("authorName", authors);
  
      if (programName.trim().equals(program.trim())) {
        programChaptersList.add(programChapters);
      }
    }
  
    product.put("programName", program);
    product.put("programThumbImageURL", "");
    product.put("programeURL", "");
    product.put("programChapters", programChaptersList);
  
    collection.insert(product);
    System.out.println("*#*#*#*#*#");
  }
  }
  </code></pre>
  
  <p>I hope this is the part went wrong. Need to store all chapterNames in an array and compare with each upcoming value and according to that create new objects and store it in a list</p>
  
  <pre><code>List&lt;String&gt; authors = new ArrayList&lt;String&gt;();
  
  programChapters.put("chapterName", chapterName);
  
  authors.add(authorName);
  programChapters.put("authorName", authors);
  </code></pre>
  
  <p>Can someone suggest me, available solutions :-)</p>