0

So I am currently making a REST app where I receive excel files through requests ,then I use Tablesaw to read excel files and MongoTemplate to save tables in MongoDb . I created this simple function to map results in a way that it looks clean when I save it in MongoDB.

    public static List<Map<String, Object>> createMapFromExcelTable(Table table) {
        List<String> colNames= table.columnNames();

        List<Map<String,Object>> mapList = new ArrayList<>();

        Map<String,Object> recordMap;
        Map<String,Object> fieldsMap;

        for (int i = 0; i < table.rowCount(); i++) {
            recordMap=new HashMap<>();
            fieldsMap=new HashMap<>();

            String id= IdUtil.getShortId();
            recordMap.put("recordId",id);

            for (String colName : colNames) {
                fieldsMap.put(colName,table.row(i).getObject(colName));
            }

            recordMap.put("fields",fieldsMap);

            mapList.add(recordMap);
        }
        return mapList;
    }

Here is the example of what saved documents look like in MongoDB https://prnt.sc/2388js5

Any tips on how to improve this function in any way? Something just doesnt feel right, I think I can make it more optimized and clean. Especially this part

            recordMap=new HashMap<>();
            fieldsMap=new HashMap<>();

Dont like the idea of creating new HashMaps but I guess cant escape that .

  • You can possibly load data from Excel data (as CSV) using `mongoimport` command-line tool. – prasad_ Dec 16 '21 at 07:12
  • Well I am making a REST app where people upload excel files and send a request . So I dont fully understand how would I use mongoimport . I will edit my question and make it clear that I am making a REST app. – Nikoloz Latsabidze Dec 16 '21 at 07:19

0 Answers0